Home > Back-end > Human confused problems (false)? Shallow copy of the relevant problem... Why the same code to run th
Human confused problems (false)? Shallow copy of the relevant problem... Why the same code to run th
Time:09-21
Today a friend asked me questions, there was a confused situation, is the same code, that I can run, it will collapse, the ta Later found to be shallow copy of the related problems, the problem of the code is as follows:
# include & lt; Iostream> Struct Node { The int data; Struct Node * next; }; Int main () { Node * p=new Node; Node * q=new Node; Q=p; Free (p); Free (q); return 0; }
Q=p should be a shallow copy, i.e., q and p points to the same block of memory,
If free (p) to perform free (q), because the q has been free to free the memory should be morally error,
And perform in a friend's computer do error, at the end of the I here is normal (no error, return 0)??
Ask bosses what this may be the cause?
CodePudding user response:
First, for free is a NULL pointer, will not make a mistake, So if you free environment (p), the p rewriting is null? If so, will not make a mistake,
CodePudding user response:
No operation error does not mean that the program is no problem, have a certain probability of In real environment is likely to lead to other place to crash, I met before, it is difficult to find