Here is the code:
int main()
{
char str[] = {'a','b','c',' ','d','e',' ',' ','f',' ',' ',' ','g','h','i',' ',' ',' ',' ','j','k'};
cout << "Len = " << strlen(str) << endl;
char* cstr = new char[strlen(str)];
strcpy(cstr, str);
cstr[5] = '\0';
cout << "Len= " << strlen(cstr) << endl;
return 0;
}
Result console:
Len = 21
Len= 5
As you can see, Len from cstr has changed. This means that the cstr memory area is free. Correctly?
source
share