Can you create a Vector of structs in C++? I need help with a unknown error pertaining to this subject.

Started by
21 comments, last by uglybdavis 11 years, 4 months ago
Alright I got it to work using pointers. Thanks for the assistance.
Advertisement
There's absolutely no need to apologize.

Memory management in C++ is probably THE thing that a lot of people never get right and they just give up trying to learn C++ after a while. The basics are not THAT difficult, though:

The value of a variable has a "lifetime". This lifetime ends with the end of the current scope. I.e. it ends when the next "}" is encountered. So this:


if (true) {
Rectanlge rect;
}
cout << rect.getX();


will lead to a compiler error since rect is not valid anymore at the point where rect.getX() is called. In the moment the closing "}" is encountered the Rectangle object called "rect" is destroyed. [size=2](Keep reading, I know, it's not a great example, since this is a compiler error, and the problem at hand is a runtime problem, just trying to get a feeling for scoping here...)

So for your case this means:

for(int j=0;j<8;j++){
int ya=(j*50)-1;
Rectanglee rect(Point(i,j),50,50);
rects.push_back(&rect);
}


every time the closing brace of the for-loop is hit, the object formerly know as "rect" is destroyed.

Now, your code saves the address of that object inside the vector. Too bad... the object is dead when the for-loop is left... So that address is nothing but garbage.So in your vector there's a whole bunch (well 8...) addresses that used to be Rectangle objects but are not anymore...

So now, let's look at the "new" keyword. The rectangless we've created so far were on the stack. Objects on the stack behave like I just said. They are deleted once the variable goes out of scope. The second kind of memory is the "Heap". When you use "new" this will create a new object on the heap. That object is valid and available until
a) the program is terminated
or
b) you use "delete" to ... well... delete it.
So this:

for(int j=0;j<8;j++){
int ya=(j*50)-1;
Rectanglee* rect = new Rectangle(Point(i,j),50,50);
rects.push_back(rect);
}

[size=2](note: used a temporary variable to make it clearer) will create 8 new Rectangle object on the HEAP that will be valid even after exiting the for-loop. Now... This may be a problem. Who will ever invalidate (i.e. delete those Rectangles)? They will be on the heap occupying memory until the program is exited (thanks to modern operating systems) or until you explicitly remove them using "delete".

So, if you want to get rid of those rectangles (all of them) this would work:

for(int j=0;j<8;j++){
delete rects;
}
rects.clear();

This first deletes all the Rectangle objects that you've created on the heap and then clear the vector (because since we've just deleted all the rectangles, it contains nothing but a lot of addresses that formerly pointed to an object of type "Rectangle").

And this is exactly where smart pointers come in play... smart pointers help you to get rid of the burden to manually delete those objects that you've created on the heap, as they will automagically delete the object when there is no smart pointer left pointing at that object (it does not work all of the time [circular dependencies are a problem], but for a start it is pretty good)...


Hope this somewhat helps to get rid of the headache....
@brx +1

Great breakdown.

This topic is closed to new replies.

Advertisement