1

I'm designing a graphics library which only works with a bitmap that is stored in memory.

Since a bitmap is 2-dimensional (x and y or columns and rows), i can use a 2-dimensional array.

//PIXEL_BIT is a user defined type to store RGB values of a single pixel
PIXEL_BIT buffer[1366][768];

From my understanding, C++ doesn't play nice with two dimensional arrays. Especially when it comes to dynamic 2-d arrays. So I believe, 2-dimensional arrays are not so cool to use as a buffer (or an in memory bitmap).

Another method to declare a buffer would be:

PIXEL_BIT *buffer = new PIXEL_BIT[1366 * 768];

I think this method is more efficient. So my question is, If you were to store a bitmap in memory what kind of a buffer would you use ? What's the best way to store a bitmap in memory (I think a block of memory that can be randomly accessed) ?

[Edit] I do understand what the heap and the stack are. And you should probably see this question: How do I declare a 2d array in C++ using new?

Community
  • 1
  • 1
  • in which way doesn't C++ play well with arrays? I'd try both: measure the run times and compare – jev Sep 30 '13 at 08:24
  • @jev I meant two-dimensional arrays doesn't fit the situation and i stated that one dimensional arrays would be more efficient than two-dimensional arrays. Since the computer memory is linear and a 2-d array us just an abstraction, it would be not be more efficient to use 2-d arrays than single dimensional arrays. –  Sep 30 '13 at 08:27

3 Answers3

0

Two dimensional array creates an array of pointers to plain arrays. This would cause some complications in memory management and/or streaming of such arrays. I'd prefer doing it the second way, creating one dimensional array, and using a few macros or inline functions to access elements. In fact, it would be better to create a structure, that has members to specify "width" and "heights" of buffer.

struct PIXEL_BIT_MAP {
PIXEL_BIT *buffer;
size_t height;
size_t width;
}
wirm
  • 100
  • 5
  • Yes 2d arrays are an array of pointers to arrays. And This is exactly what I thought. –  Sep 30 '13 at 08:45
  • @wirm *"Two dimensional array creates an array of pointers to plain arrays."*. Not necessarily true, `PIXEL_BIT buffer[1366][768];` creates *array of arrays*. There are no pointers involved. – user694733 Sep 30 '13 at 10:37
  • @user694733 first of all, array is a pointer, unless it is statically allocated, which is not our case. If we create two dimensional dynamic array, then it will be `PIXEL_BIT **array;` – wirm Sep 30 '13 at 11:17
  • @wirm Both code lines OP posted do not have any array of pointers. Only pointer is on his second line `PIXEL_BIT *buffer`. [Array is **not** a pointer.](http://stackoverflow.com/a/1641963/694733). – user694733 Sep 30 '13 at 11:24
0

The first way (PIXEL_BIT buffer[1366][768];) allocates memory on stack, which means after your current function returns, that block of memory becomes unusable.

You should allocate memory on heap to store bitmaps, using either new operator or malloc(). There is no obvious difference between these two methods, and both allocate "a block of memory that can be randomly accessed".

Chen
  • 1,170
  • 7
  • 12
  • 1
    @DavidSebastian You probably don't want to use the dynamic 2D array in that answer. It is better to store a whole bitmap in a block of continuous memory instead of storing it line by line. You won't lose any performance or functionality. – Chen Sep 30 '13 at 08:47
0

Here are two easy ways to make a 2d array. You can wrap the latter in a nice class, so you don't have to provide the index calculation each time. I would think that the second is faster, because all the data is nearby in memory.

vector<vector<T>> myvec(Y, vector<T>(X));
myvec[y][x] = ...;

vector<T> myvec(X * Y);
myvec(y * X + x) = ...;
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91