C++ dynamic arrays question


#1

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.


#2

//! \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;
}


#3

… <double post deleted>


#4

Thanks for the prompt answer. Trying it out now.:slight_smile:


#5

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;

}

-M


#6

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.