Recommended Posts

hey there, I''m sure this question is really easy but I can''t seem to find a solution anywhere.
I''m creating a dynamic array of a number of blocks like this:
m_Blocks = new cBlock[NUMBEROFBLOCKS];
but the problem is that I need to pass an argument to each of the constructors, in this case a pointer to my display.
If I was just creating one block I would do it like this:
m_Blocks = new cBlock(m_Display);
but for arrays, do I put the argument after the [] or before or where?
See! Very easy Thank you if you can help me.
I''m using vc++ 6.0.

Share this post

Link to post

Share on other sites

This poses an interesting question, how DO vectors allocate memory? For example, when you resize or push_back, does it reallocate the whole vector or work like a linked list, assigning new memory all over the place and linking them in each element.

What I''m asking is, does a vector work like an array, and if so, does a sizeof( vectorOfInts[0] ) == sizeof( int ) ?

Share this post

Link to post

Share on other sites

Vectors use continues memory, AFAIK. It uses an std::allocator to allocate the memory. The allocator probably reserves 2mb of data, and the vector takes what it needs. If it needs more, the allocator already have memory reserved, and it uses that memory.

I assume, that when the allocator runs out of memory, it will allocate a new block, and move all other data into that.