user99999991
user99999991

Reputation: 1421

C++ Arena Allocator Free Memory is Always Half

const int size = 50000;
char pool[size];

int freeMemory(void)
{
int freemem = 0;
for(int i = 0; i < size; i++)
{
    if(pool[i] == NULL)
    {
        freemem++;
    }
    i++;
}

return freemem;
}

If I call freeMemory(), the size returned is always HALF of what originally declared. 25000 specifically here Even if I'm not initializing anything in the pool itself. Why is this? Do I need to initialize something first?

void initialize(void)
{
//Don't quite understand if I need to initialize something here? 
}

And I'm trying to avoid using the new operator too. The reason being is that I'm trying to manually manage the memory inside the pool.

Upvotes: 2

Views: 426

Answers (1)

Jorge Israel Pe&#241;a
Jorge Israel Pe&#241;a

Reputation: 38616

Because you have an extra i++; statement in your for loop. So each iteration actually increments the iterator twice, effectively making you only loop half the times.

Remove the i++; line at the end of your for loop.

Upvotes: 2

Related Questions