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.

CodePudding user response:

refer to the original poster to learn code is the reply of hobbies:
can you tell me whether the allocated memory of static pointer ar 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;
}

Your free is released in the main, though malloc is a function in the test, no problem

CodePudding user response:

Has been released, you can output the content see
 # include & lt; Stdio. H> 
#include
Int * test (int);
Int main ()
{
Int * p=NULL;
Int * d=NULL;
Int b=5;
Printf (" \ n \ n % d % d ", d, p);
The scanf (" % d ", & amp; b);
D=p=test (b);
For (int I=0; I & lt; b; I++) {
Printf (" % d ", * (p));
}
Printf (" \ n \ n % d % d % d % d ", d, p * d * p).
Free (d);
Printf (" \ n \ n % d % d % d % d ", d, p * d * p).
return 0;
}

Int * test (int b)
{
Static int * ar;
Ar=(int *) malloc (sizeof (int) * b);
for (int i=0; I & lt; b; I++)
Ar [I]=I;
Printf (" % d % d \ n ", ar, * ar);
Return the ar;
}



The results are as follows:
0 0
3
42604304
0 0 0
42604304 42604304 0 0

42604304 42604304 42603968

CodePudding user response:

The landlord to get the static definition is arr pointer variable, it just shows a pointer variable will not release as the end of the function call, and should be arr variable is in the application of data, it starts from the definition of life cycle know program exits, but it has nothing to do with malloc oh, is that the contents of this pointer to can change,

CodePudding user response:

reference 7 building self-confidence boy reply:
with the landlord to get the static definition is arr pointer variable, it just shows a pointer variable will not release as the end of the function call, and should be arr variable is in the application of data, it starts from the definition of life cycle know program exits, but it has nothing to do with malloc oh, is that the contents of this pointer to can change,

Question no idea of the necessity of the static, behind myself with posts have been illustrated to the problems, and also made explanation about the release, c primer plus the content of the questions have yet to see, later found the book description is clear, but thank you for answering questions
  • Related