Reputation: 395
struct listblock
{
struct listblock*next;
struct listnode*head;
int countNode;
}
Author said Allocating its single node will take (12 bytes + 8 bytes of overhead) i didn't understand what this 8 bytes of overhead mean and how did he calculate it?
Upvotes: 0
Views: 324
Reputation: 726929
When you call free
on the object that you have previously malloc
-ed, the memory management system needs to do some "bookkeeping" to add the chunk of memory that you are returning back to the pool of unallocated memory. Specifically, it needs to know how big a chunk you have requested, so that it could do realloc
into the same block if possible. The actual data is specific to malloc
's implementation, but it typically includes a link to other memory blocks managed by the dynamic allocator.
The bookkeeping data has to be stored somewhere for each memory block returned to you from malloc
. It may be stored within the memory block returned to you, or in a separate place known to the memory allocator, but there will be at least two numbers representing the requested and the allocated size.
Where Do malloc()
/ free()
Store Allocated Sizes and Addresses?
This is what the author calls an overhead: when you request 12 bytes, the system gives you the 12 bytes, and also uses up two 4-byte chunks (the size of the block and a link to the next block) for its own bookkeeping, for a total of 20 bytes.
Upvotes: 3