(glibc's) malloc and other functions will record the starting location and size of each memory block when allocating
I see you added the C++ tag, but the question seems to be only about C. In C language, the type of the return value of malloc is generally not converted. Normally there is no problem. When the size of char* is smaller than int* due to integer truncation, problems will occur (not as simple as a memory leak). But I can’t seem to find a system in which these two pointers have different sizes, right?
Never feasible because it is logically wrong. 1. Another thread can use the memory block before you access it; 2. The memory allocator can clear the released memory for safety reasons; 3. Other situations.
The answers to 1 and 3 are very clear, let me add the second point:
In the C language standard library, the free function prototype is:
void free (void* ptr);
In the C language standard, void* and all pointers to various data are consistent with char*, including size and alignment.
So there is no problem in performing various conversions between data pointers.
But please note: The C language standard does not stipulate that data pointers and function pointers are the same. This is undefined behavior.
The memory block allocated by malloc() contains a header block, which is used to record information such as the size of the allocated memory. free() will read this block and release the memory.
Allocate int type memory size, but only release half of the space (assume int is 2 bytes here), so when the excess memory continues to increase and fills up the memory space, it will cause a memory leak.
This release method is wrong. Are you sure this is what is written in the book? The correct way to write it should be:
for(p = head; p != NULL; p = q){
q = p->next;
free(p);
}
char*
is smaller thanint*
due to integer truncation, problems will occur (not as simple as a memory leak). But I can’t seem to find a system in which these two pointers have different sizes, right?The answers to 1 and 3 are very clear, let me add the second point:
In the C language standard library, the free function prototype is:
void free (void* ptr);
In the C language standard, void* and all pointers to various data are consistent with char*, including size and alignment.
So there is no problem in performing various conversions between data pointers.
But please note: The C language standard does not stipulate that data pointers and function pointers are the same. This is undefined behavior.
The memory block allocated by malloc() contains a
header block
, which is used to record information such as the size of the allocated memory. free() will read this block and release the memory.Allocate int type memory size, but only release half of the space (assume int is 2 bytes here), so when the excess memory continues to increase and fills up the memory space, it will cause a memory leak.
This release method is wrong. Are you sure this is what is written in the book? The correct way to write it should be: