new operator - C++ new memory allocation fragmentation -


i trying @ behavior of new allocator , why doesn't place data contiguously.

my code:

struct ci {     char c;     int i; }  template <typename t> void memtest() {     t * plast = new t();     for(int = 0; < 20; ++i) {          t * pnew = new t();          cout << (pnew - plast) << " ";          plast = pnew;     } } 

so ran char, int, ci. allocations fixed length last, there odd jumps 1 available block another.

sizeof(char) : 1
average jump: 64 bytes

sizeof(int): 4
average jump: 16

sizeof(ci): 8 (int has placed on 4 byte align)
average jump: 9

can explain why allocator fragmenting memory this? why jump char larger ints , structure contains both int , char.

there 2 issues:

  • most allocators store additional data prior start of block (typically block size , couple of pointers)

  • there alignment requirements, e.g. linux typically allocates @ least 8 byte boundary, os x allocates @ least 16 byte boundary

so you'll kind of gap between successive allocations.

of course should never rely on specific behaviour this, implementation free pleases.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -