XM Magdalena 3D print, GGeorgy (3D)
LC #42 Pipers Alley

View Full Version : C++ dynamic arrays question

04-01-2007, 11:04 AM
Could somebody post example code for creating a

800 x 600 x 3 float array (800x600xRGB with 32 bits per channel)

using the C++ STL vector class and deleting it when needed?

I'm relatively new to C++ and some of the examples I've found on the web have left me scratching my head. Thanks in advance for any help provided.

04-01-2007, 11:35 AM
//! \file Homework.cpp

#include <vector>

int main()
// declare the vector
std::vector<float> rgbArray;

// resize it to the dimensions you want
rgbArray.resize( 800 * 600 * 3 );

// ... do something cool

// resize the vector to 0 to clear and delete
rgbArray.resize( 0 );

return 0;

04-01-2007, 05:02 PM
.... <double post deleted>

04-01-2007, 05:03 PM
Thanks for the prompt answer. Trying it out now.:)

04-17-2007, 03:55 AM
Sorry, I like a little organization in my code:

#include <vector>

// Not a very good idea in C++, but ok for this example
#define X_RES 800
#define Y_RES 600

// Store a RGB structure so you don't have to
// "think" about where you are in the array.
typedef struct {
float r;
float g;
float b;
} RGB;

int main()

std::vector<RGB> rgbArray;

rgbArray.resize(X_RES * Y_RES);

// do something cool.. but now, you
// can index the array with meaning!
// rgbArray[0].r = some red value for the first pixel;
// rgbArray[0].g = some green value for the first pixel;
// rgbArray[0].b = some blue value for the first pixel;

rgbArray.resize( 0 );

return 0;



CGTalk Moderation
04-17-2007, 03:55 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.