Search code examples
c++allegro

C++ (Allegro Library) Bitmap Two-Dimensional Array Question


I have a quick question. I am using C++ with the Allegro library.

If I make the following declaration BITMAP* blocks[600][14];, is it going to be wasted space if I only occasionally am using all 14 all of the second dimension or is that space only used when I specifically declare that part of the array?

For example:

BITMAP* blocks[600][14];
blocks[0][0] = load_bitmap("brick.bmp", NULL);
blocks[1][0] = load_bitmap("brick2.bmp", NULL);

Am I 'wasting' space by not using blocks[0][1], blocks[0][2] etc.?

Thanks,

Will.


Solution

  • In C++, arrays are contiguous so yes, you are 'wasting' the additional elements if you don't use them.

    However, what you are wasting is only a single pointer (8 bytes on a 64-bit machine); not the actual bitmap data. So even if you use only one BITMAP* element in your 600x14 array you're wasting ~67kB; which isn't a massive amount in a modern desktop machine.