parth
parth

Reputation: 272

dynamically allocating arrays

I'm working on an OpenGL/C++ program where I need to store coordinates as and when I plot them in my window. But since the number of points plotted is undefined, I cant fix up the size of the array. Is it possible to dynamically allocate space to an array? Can I use any other data structures to accomplish the same?

Upvotes: 1

Views: 222

Answers (2)

PaperBirdMaster
PaperBirdMaster

Reputation: 13298

As suggested, an std::vector is a good choice and this container assures contiguous memory, this would be useful for pass it as a coordinates buffer:

std::vector<Coordinate> coordinates;
coordinates.push_back(Coordinate(0,0));
coordinates.push_back(Coordinate(1,1));
// ... add more coordinates...

RenderBuffer(coordinates.data());

As long as the memory suits the buffer format, of course.

But, by the fact of the use of contiguous memory, the insertion method could be expensive due the reallocation of memory blocks; if this could be a problem you can get rid of it reserving a size once if you know the maximum/minimum requeriments of memory:

// Create 100 instances of Coordinate and fill all instances with Coordinate(0, 0)
std::vector<Coordinate> coordinates(100, Coordinate(0, 0));

coordinates[0] = Coordinate(0,0);
coordinates[1] = Coordinate(1,1);
// ... add more coordinates...

RenderBuffer(coordinates.data());

But if you do that, there's no difference of using an array:

// Create 100 instances of Coordinate and fill all instances with Coordinate(0, 0)
Coordinate[100];

coordinates[0] = Coordinate(0,0);
coordinates[1] = Coordinate(1,1);
// ... add more coordinates...

RenderBuffer(coordinates);

With one exception, the array cannot change it's size if it's needed, but the std::vector can be resized:

// Create 100 instances of Coordinate and fill all instances with Coordinate(0, 0)
std::vector<Coordinate> coordinates(100, Coordinate(0, 0));

coordinates[0] = Coordinate(0,0);
coordinates[1] = Coordinate(1,1);
// ... add lots of lots of lots of coordinates...

// Damn! We need more Coordinates, let's add more without worrying about resizing:
coordinates.push_back(Coordinate(0,0));
coordinates.push_back(Coordinate(1,1));    

RenderBuffer(coordinates.data());

If the contiguous memory isn't needed, you must consider to use other containers like std::list, the insertion method is the same std::list<>::push_back but the insertions of this kind of container aren't expensive at all (at least not that expensive as vector).

You can also use the method std::vector<>::resize for resizing once instead of resizing for some of the std::vector<>::push_back calls.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258648

Yes, use a std::vector instead.

Combined with push_back you can dynamically increase the number of elements.

std::vector<Coordinate> coordinates;
coordinates.push_back(Coordinate(0,0));
coordinates.push_back(Coordinate(1,1));

You can access the elements same as you would with an array:

coordinates[0], coordinates[1]...

Upvotes: 6

Related Questions