code show as below:
void CopyStr(char *&destination, char *&source) {
int sz = strlen(source) + 1;//此处引发异常!!
destination = new char[sz];
for (unsigned i = 0; source[i] != 'rrreee'; i++)
destination[i] = source[i];
destination[sz - 1] = 'rrreee';
return;
}
Data::Data(Data &adata) {
CopyStr(adata.P_name, P_name);
CopyStr(adata.address, address);
CopyStr(adata.number, number);
}
Code Description: The Data class now has three char* members, namely P_name, address and number.
Compiled picture:
Please give me the answer, thank you! !
First, the parameter char *&source is changed to
char *const &source
, and secondly, CopyStr(adata.P_name, P_name) is changed to CopyStr(P_name, adata.P_name).This is the code I tested, it can be run directly:
Where does the second parameter (such as P_name) come from when calling CopyStr?
vs will fill the uninitialized memory area with 0xCCCCCCCC (this is also the origin of
scalper
);Considering that you accessed 0xCCCCCCCC, either you passed in an illegal pointer, or this
char*
The string corresponding to the pointer does not end with '0';You can try to output the value of
source
, and then try to output the data pointed to by the pointer byte by byte