Boost logo

Boost :

From: ÀîÖرó (lichongbin2004_at_[hidden])
Date: 2024-04-26 13:55:34


I want to use singleton_pool to manage memory. This is just an example which tests it.
My question: the first time i allocate 100 spot object, and free them. If i allocate some spot objects, and the number of them is less than 100. no more memory is needed, there is no need to allocate memory from os again. but the fact is another memory block is allocated.
The first block is 4016 bytes (sizeof(Spot) = 40), the second block is 8016 bytes. This is not as my imagine. Is there something wrong with these code?

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     std::cout << "size of Spot " << sizeof(Spot) << " bytes\n";
    std::cout << "test singleton_pool firt time\n";
    const size_t NumSpots = 100;

    Spot* p1 = static_cast<Spot*>(spot_pool::ordered_malloc(NumSpots));
    for (size_t i = 0; i < NumSpots; i++)
    {
        p1[i].x = 1;
        p1[i].y = 1;
    }
    spot_pool::ordered_free(p1);

    std::cout << "test singleton_pool second time\n";
    Spot* p2 = static_cast<Spot*>(spot_pool::ordered_malloc(NumSpots/2));
    for (size_t i = 0; i < NumSpots/2; i++)
    {
        p2[i].x = 2;
        p2[i].y = 2;
    }
    spot_pool::ordered_free(p2);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk