Home > Back-end > Excuse me for the distribution of the static cursor ar memory is released?
Excuse me for the distribution of the static cursor ar memory is released?
Time:10-22
Excuse me for the distribution of the static cursor ar memory is released?
# include & lt; Stdio. H> #include Int * test (int); Int main () { Int * p=NULL; Int * d=NULL; Int b=5; The scanf (" % d ", & amp; b); D=p=test (b); for(int i=0; iPrintf (" % d ", * p++); Free (d); return 0; }
Int * test (int b) { Static int * ar; Ar=(int *) malloc (sizeof (int) * b); for(int i=0; iAr [I]=I; Return the ar; }
CodePudding user response:
During the program is running will not release, refer to the address will not be changed, is it the content may change, but will not change to address
CodePudding user response:
Just check c primer plus there is clear, free release can not malloc application memory pointer variable, as long as the free parameters of the pointer is the address of the application, then made a guess system recorded the address and size of malloc application information, just find a passage on the Internet also verified the own guess, "the release of why can correct the address of the correct size, some compilers can such operation, malloc will before the address of the first 8 bytes high deposit 4 page id, low deposit 4 size, free can take from here to the dao of size, rely on the compiler's different implementation methods, only for your reference," The above application can not use static decorate, can also be executed correctly, if using arrays in the test, you must use the static, but static and does not support variable-length array, must use the pointer to allocate memory way at this time,
CodePudding user response:
The main d and the test of ar refer to the same piece of memory, release d is to release the ar
CodePudding user response:
Debugged, I think it's been released.. Because all three pointer is point to the same area.