C ++ new memory fragmentation

I tried to look at the behavior of the new allocator and why it does not put the data in contact.

My code is:

struct ci {
    char c;
    int i;
}

template <typename T>
void memTest()
{
    T * pLast = new T();
    for(int i = 0; i < 20; ++i) {
         T * pNew = new T();
         cout << (pNew - pLast) << " ";
         pLast = pNew;
    }
}

So, I ran this with char, int, ci. Most distributions were fixed in length from the latter, sometimes there were odd transitions from one available block to another.

sizeof (char): 1
Average jump: 64 bytes

sizeof (int): 4
Average jump: 16

sizeof (ci): 8 (int should be placed in 4-byte align)
Average jump: 9

Can someone explain why the allocator is so fragmenting the memory? Also why the leap for char is much bigger than ints and the structure that contains both int and char.

+4
6

:

  • ( )

  • - 8- .

, .

, - - , , .

+10

, , (char *), sizeof (T).

+6

, .

, new. , (boost:: pool, , ..)

- , , K & R , , new .

+3

, . . , .

, "" . Linux- , , . , , , .

+2

, , . , , .

,, , , malloc() new. new ( ++ FAQ Lite).

( , , malloc() new T[] . , .)

0

boost , boost:: simple_segregated_storage

Creates a copy of slides of free and used blocks of the same size. As long as you select only your block size, you don't get any external fragmentation (although you can get internal fragmentation if your block size is larger than the requested size.) It also runs O (1) if you use it in this manner. Great for small distributions, among which are common with the programming pattern.

0
source

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


All Articles