MipsMoreLikeWhips
MipsMoreLikeWhips

Reputation: 147

Dynamic Allocation between c-strings and (ints,doubles,floats)

I was making a dynamically allocated array for c-strings. When it get to me resizing my arrays, I would use strcpy,strlen, and strcat to help with the process. This got me thinking that when I make my dynamically allocated array for c-strings, I never have to declare and define a copy constructor and an assignment operator for the c-string arrays. But, if I were to make a dynamically allocated array for a double or float I would have to declare and define a copy constructor to get that deep copy of the array and declare and define an assignment operator to allow someone to make a copy of an instance.

My question is since strlen,strcpy, and strcat are predefined functions in the C language do they automatically take care of things like deep copies and making copies of instances in dynamic arrays or would it be smarter to define and declare a copy constructor and an assignment operator?

If this is to vague, I can elaborate.

Upvotes: 1

Views: 88

Answers (2)

MrEricSir
MrEricSir

Reputation: 8242

My question is since strlen,strcpy, and strcat are predefined functions in the C++ language do they automatically take care of things like deep copies and making copies of instances in dynamic arrays...?

Those are C functions, not C++ functions! And no, they do not automatically perform deep copies for you.

Upvotes: 0

jhoffman0x
jhoffman0x

Reputation: 795

C++ has std::vector and std::string for automatically handling such things. You seem to think that a dynamically allocated array of characters automatically gets deep copied, but this is incorrect. The type of your dynamically allocated arrays does not change their copy/assignment behavior; the default copy/assignment provides shallow copies. You would either need to define a copy constructor and assignment operator, or just rely on the more robust features of the STL.

Upvotes: 1

Related Questions