c - Will the Heap Scope be freed successfully while the Pointer Value was changed? -


for example:

void heaptest(){        int *a;        a=(int*)malloc(1024*4);          int i=1024;        while(i--){                         *a=i;                   //printf("%d",*a);                    a++;                  }        free(a);                 } 

when 'a' used pointer, assume points address "0x20000". , scope of heap area 0x20000 0x21000. totally 4096 bytes.

after while loop, 'a' pointed 0x21004, out of scope of defined heap. if free heap using

free(a) 

will heap freed successfully?

for observation, when use func in visual studio. show

invalid address specified rtlvalidateheap 

and value of 0x21004 before free() operation whenever whether there printf() function in while loop.

when use function on keil-mdk stm32f7(cortex m7), shows nothing before free operation. value of 'a' become 0x00000;

but when add printf() function shown in code. value of 'a' initial value 0x20000.

so, final question is, could change value of heap pointer? or assign initial value every time before free() operation?

will heap freed successfully?

it impossible say. invoke undefined behavior passing pointer value free() not returned malloc() or 1 of other allocation functions. could turn out manifest freeing block pointer points, more produces error, or worse: silent memory corruption.

note in particular pointer value matters. variable, if any, in store pointer value has nothing directly it.

could change value of heap pointer?

in sense pointer value, no, can no more change can change value 1. can cause variable in value stored instead contain different value. may able in way allows recover original value, retain ability free allocated memory.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -