• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# How much memory is really allocated?

## Recommended Posts

Promit    13246
When you allocate a block of memory on the heap, say: int* p = new int[1000]; C++ allocates that block, plus a little bit after it that contains information on deleting it later (which is why overwriting is a Bad Thing). So generally, how much extra is stuck on the end, and what exactly is in the extra info block?

##### Share on other sites
Kylotan    9870
I was under the impression that implementations were more likely to put size information before the block rather than after it. Technically they only need to reserve a size_t, which is 4 bytes. In debug mode you''ll also find some compilers pad both sides of the allocated block with special values so that they can detect overwrites.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

##### Share on other sites
fagiano    307
depends on the allocator architecture
a heap chunk header could look something like that

struct ChunkHeader {	int realsize; //size plus overhead	int prevoffset; //realtive offset to the previous block in the same page	int size; //payload size}

also consider that usually a general pourpose allocator has a granularity of 32/64 bytes so everything is at least 32 + sizeof ChunkHeader.

Alberto

-----------------------------
The programming language Squirrel
http://squirrel.sourceforge.net