best way to create a dynamic 2d array to store image data

I have a struct containing 3 bytes, one for each pixel, a 2D array of these is to be written into a bitmap file generated the program. The size of this array is not known at compile time.

At present I am declaring a double pointer using ** symbol and then use the 'new' keyword to create the dynamic 2d array. However, this is how it would be done in C.

What is a better way to doing this in C++?
Instead of using new and delete, use smart pointers.

https://en.wikipedia.org/wiki/Smart_pointer
You can use std::vector and better organize it in a 1d array:
1
2
3
4
5
std::vector<pixel> bm(x_max * y_max);

...

  pixel px = bm[y * x_max + x];
It might be useful to think of the pixel data as a contiguous block of memory (in effect a one-dimensional array) as this could make it possible to write the data to the file (or read it from a file) in a single write (or read) operation.

Bear in mind that each row of pixel data needs to be padded to a multiple of 4 bytes.

A C++ class could be used to contain the pixel data, and facilitate access by row and column number.
Topic archived. No new replies allowed.