why use the costom copy string function?

Started by
1 comment, last by Jemburula 18 years, 11 months ago
void CopyString(const char* input, char** output) { if( input ) { UINT length = (UINT)::strlen(input) + 1; // add 1 for terminating null charater. *output = new char[length]; ::strcpy(*output, input); } else { *output = 0; } } the stdio.h : strcpy() is not good or not work?
Advertisement
The code you have is a function that wraps strcpy(). The difference is that the wrapper function automatically allocates memory for the new string, while a direct call to strcpy() needs to have the memory allocated for it.
Ra
Copy constructor is also used alot for your own classes, it prevents a shallow copy such that when Object1 is copied by Object2 they aren't pointing to the same data in memory so that if Object1 were deleted, attempting to use Object2 results in a runtime error I think?
Goodluck
What we do in life... Echoes in eternity

This topic is closed to new replies.

Advertisement