Can I free () by referencing the offset pointer?

Possible duplicate:
Does the pointer to free () point to the beginning of the memory block, or can it point to the interior?

I allocate some memory on the heap using malloc(). I add an offset to the pointer and return it to the calling function return ptr+(sizeof(char)*4)), where this return value is stored, for example, in ptrReturned.

The big question is: when will I free(ptrReturned), which is offset from the beginning of the selected block by sizeof(char)*4, free the entire block or free the memory from the offset to the end of the selected block? The compiler I use is MSVC ++ 2008.

If the highlighted addresses are stored in a linked list and there are pointers to previous elements, then it should free the entire block from which the previous pointer is NULL, to where the next pointer is NULL, right?

I tried debugging and observing the memory, but I can't get it to overwrite the freed block space. Is there a way to explicitly try to allocate space on the heap at a specific address and possibly get an exception to the attempt to allocate already allocated memory? That way, I can find out if, of course, there is no simpler answer.

Hope the question is clear, if I lack any information, please let me know in the comments. Thank.

+3
source share
2 answers

free(), malloc(), undefined. , , .

+8

:

, , calloc, malloc realloc , realloc, undefined.

+2

Source: https://habr.com/ru/post/1786602/


All Articles