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:

Double free if it is in the debug version of the model, the general will collapse, because the debug version of the memory allocation of additional information, but in the release version, not necessarily will collapse, and the treatment of some compilers may also have relations, so the release version of the error it is sometimes hard to find, and not stable

CodePudding user response:

This has nothing to do with shallow deep copy, are on the same piece of memory has released two
You can refer to the following post, see free source code
https://www.cnblogs.com/hanyonglu/archive/2011/04/28/2031271.html
Application memory is attached additional memory management information memory space, and release management information memory space together to modify the release, and release will change again the management information memory space (the space release already belongs to the waste of memory, for the first time, so modify it may lead to conflict)
In addition, your application is free memory function also is not standard, new to delete, malloc for free, and over the use may cause problems

CodePudding user response:

First of all, your code is an error in VS2015 c + + environment;
Secondly, like friends upstairs said, new must correspond to delete, to delete is still an error, reason is that q and p points to the same block of memory,

CodePudding user response:

https://bbs.csdn.net/topics/396632127
  • Related