Skip to main content
GameDev.net gamedev.net
🔒 Locked

Array of pointers

Started by tordyvel Jan 7, 2004 at 5:00 PM 4 replies 600+ views
Original Post
tordyvel
tordyvel
Maybe I havn''t studied my c++ book enough, but I can''t seem to figure out how to make a dynamic array of pointers. i don''t want to do like: Object *pointers = new Object[some int]; because I just want the pointers. Or can you just do like: Object *pointers; pointers[0] = Object_one; pointers[1] = Object_two; pointers[2] = Object_three; Thanks for your help!
Fruny
Fruny
std::vector<Object*> pointers;
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
porthios
porthios
If you want an array of pointers, there are two ways:

Object* arrayOfPointers[number_of_pointers_here];

But that will not allow any dynamics in the number of pointers, which I''d think you''d want:

Object** arrayOfPointers = new Object*[number_of_pointers_here];
arrayOfPointers[0] = new Object(constructor_parameters_here);
arrayOfPointers[1] = new Object(constructor_parameters_here);
arrayOfPointers[0]->blah = 1;

The second listing there will allow you to dynamically allocate how many objects you''d want. You could also work with just Object* I guess, up to you, but then it''s not really an "array of pointers" which is what you''re asking for ^_^
dmounty
dmounty
If you want an example of an array of pointers.. look at the second argument that main takes. That is exactly an array of pointers to arrays of char.... However, I tend to go with fruny''s suggestion... use STL .
Sta7ic
Sta7ic
Toss up between STL and a straight Object** -- depends on if you want the joys of templates in your project.

To set up a *dynamic* array of pointers, use a std::vector<object> pointers. I don''t use STL, so I don''t know J.S. on vectors.

To set up a *dynamicly generated* array of points, use "Object ** pointers = new Object *[kArraySize];", but don''t forget to nuke the memory later. Or if you need more, rebuild the array.

To "just" set up an array of pointers, use "Object * pointers[kArraySize];" Mind Porthios''s point about the immutable array length.

Personally, I''d use both an Object ** and an Object[], and use the pointer array to organize the Objects as I wanted to. You can still hit the main array''s indices by numbers, and yer prog will expect the memory to be needed -- skip some of those out-of-memory problems. This runs across the problem of not being able to adapt to attempts to spam yer engine with a mess of objects (think *craft unit limits), but that''s a design issue.

-"Sta7ic" Matt
dmounty
dmounty
if you need a container thats fast for sequetial access (but not so good for random access)... that allows for cheap insertion/deletion.. then the std::list is a good choice. If you want to add to your list of object pointers... you can just:


std::list<CObject*> objects;
object.push_back(new CObject());


When you destory the object that contains your object list.. remember to iterate through, and deallocate the memory. There are plenty of nice methods on list objects, as well as automatic support for the algorithms. Have a search around for one of the many STL guides that are around. I''m sure you''ll be an STL convert .

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.