Skip to main content
gamedev.net
🔒 Locked

[Pointer to a vector of pointers] push_back ()

Started by nGamer Jun 21, 2010 at 8:18 AM 4 replies 1.6k views
Original Post
nGamer
nGamer
What's up guys.

I have declared a pointer to a vector of pointers.

std::vector *vectorOfObjects ;
Object *thing = new Object ;
vectorOfObjects->push_back (thing) ;

However I get a run-time error that is crashing my program and I am at a loss to understand why. Does anyone have any probable causes?
SiCrane
SiCrane
Well, if this is your actual code then it would be because you never initialize vectorOfObjects. Otherwise, what does your debugger say?
Decrius
Decrius
SiCrane said it:

std::vector<Object *> vector;std::vector<Object *> *vector_pointer = &vectorvector_pointer->push_back(new Object);
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
nGamer
nGamer
Ah, I see. Thanks, guys.
no such user
no such user
Your compiler should really give you a warning for this kind of thing. For instance, this is a C4700 for MSVC, which is a level 1 warning. If you warnings enabled at all, MSVC will complain that you're using an uninitialized variable. Compilers emit warnings for a reason. Pay attention to them. If your compiler didn't give you a warning for this, then it's time to get a new compiler.
Decrius
Decrius
Quote:
Original post by nGamer
Ah, I see. Thanks, guys.


If you're going to use it beyond the current scope, do this:

std::vector<Object *> *vector_pointer = new std::vector<Object *>vector_pointer->push_back(new Object);delete vector_pointer;


The local variable (first line in my first reply) will be destroyed at the end of the scope (that is, when a closing bracket is met '}'). If you want to use the pointer also beyond the scope, you must use dynamic allocation as above. Don't forget to free/delete the memory at the end of the program / when you don't need it anymore!
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora

Topic Locked

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

Sign in to reply to this topic.