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

CodePudding user response:

reference 1/f, the truth is right or wrong 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,

Thanks for 1st floor reply
Just to have a try to see that after the free is not NULL...

CodePudding user response:

references in winter is far from the second floor spring? Reply:
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

I think here is indeed a program is problematic, was wondering why did not lead to crash or error, this is the problem that the compiler?

CodePudding user response:

Has nothing to do with the compiler, * (rand ()) (char *)='1' it is not necessarily cause a collapse, but may cause other potential problems, because of changes in other parts of the memory

CodePudding user response:

Free
Dynamic memory management C
Defined in a header file & lt; Stdlib. H>
Void free (void * PTR);
Solution before the allocated by malloc (), calloc (), aligned_alloc (C11) or realloc () allocation of space,

If PTR as a null pointer, the function does not operate,

If the value of PTR is not the same as before from malloc (), calloc (), realloc () or aligned_alloc () (C11) return value, the behavior is undefined,

If PTR refers to the area of memory has been allocated, the behavior is undefined, is said to PTR for a parameter called free () or realloc (), and no subsequent malloc (), calloc () or realloc () call to PTR for results,

If the free access memory through a pointer PTR, after () returns the behavior is undefined (unless another distribution function to return equal to the value of the PTR),

Free is thread-safe, it behaves as a visible area of memory access only through its parameter, rather than any static storage,

To allocate memory area called the free solution, synchronization in any order distribution function is the same or part of the same area of subsequent calls, this synchronization appear in any solution distribution function of memory access, and before any memory access distribution functions do all operations each particular area of memory allocation and solution of the distribution function has the total order alone,

CodePudding user response:

A pointer to double free belongs to standard undefined behavior, specific to see how the compiler,

CodePudding user response:

refer to 6th floor truth is right or wrong response:
free
Dynamic memory management C
Defined in a header file & lt; Stdlib. H>
Void free (void * PTR);
Solution before the allocated by malloc (), calloc (), aligned_alloc (C11) or realloc () allocation of space,

If PTR as a null pointer, the function does not operate,

If the value of PTR is not the same as before from malloc (), calloc (), realloc () or aligned_alloc () (C11) return value, the behavior is undefined,

If PTR refers to the area of memory has been allocated, the behavior is undefined, is said to PTR for a parameter called free () or realloc (), and no subsequent malloc (), calloc () or realloc () call to PTR for results,

If the free access memory through a pointer PTR, after () returns the behavior is undefined (unless another distribution function to return equal to the value of the PTR),

Free is thread-safe, it behaves as a visible area of memory access only through its parameter, rather than any static storage,

To allocate memory area called the free solution, synchronization in any order distribution function is the same or part of the same area of subsequent calls, this synchronization appear in any solution distribution function of memory access, and before any memory access distribution functions do all operations each particular area of memory allocation and solution of the distribution function has the total order separately,

Thank you! This should really is caused by undefined the compiler to handle different
  • Related