2D std::vector problem

Started by
18 comments, last by NightCreature83 12 years, 9 months ago
Hi, I need a little help with 2D vectors.

So, I have a Sprite object which has a 2D vector "coll_box" as seen below. It should hold some Vector3 pointers (x,y,z). Every row should contain info. on different "collision rectangles" (4 points).

For now I just want to add 4 dots int the first row, but I am not able to "push_back" Vector3, in the vector. The program just crashes.


std::vector < std::vector <Vector3*> > coll_box;


void eng_Collision::setColl_box(Sprite *a)
{

a->setCollisionMethod(B_RECT);

//1 2
//4 3
Vector3 *dot1 = new Vector3();
dot1->Set(a->getX(),a->getY(),0);
a->coll_box[0].push_back(dot1);

Vector3 *dot2 = new Vector3();
dot2->Set(a->getX()+a->getWidth()*a->getScale(),a->getY(),0);
a->coll_box[0].push_back(dot2);

Vector3 *dot3 = new Vector3();
dot3->Set(a->getX()+a->getWidth()*a->getScale(),a->getY()+a->getHeight()*a->getScale(),0);
a->coll_box[0].push_back(dot3);

Vector3 *dot4 = new Vector3();
dot4->Set(a->getX(),a->getY()+a->getHeight()*a->getScale(),0);
a->coll_box[0].push_back(dot4);

}//setColl_box
Advertisement
Is a->coll_box non-empty? Is this function being called while you are iterating over one of the vectors you are modifying?
If you are in debug it should tell you why it crashed.

If you are always holding 4 points, I wouldn't use a vector. Just use an array.

best guess is that your vectors don't exist yet.
a->coll_box is empty, and I call setColl_box just to add 4 Vector3 object/pointers in the first "row" (just once) .

I never use them for anything but it still crashes the program.

I mentioned that I want to have multiple collision boxes, each one defined with 4 Vector3 points/dots in a different row.

I guess I could use a 1D vector with 4n size, but I need to know how to work with 2D vectors for some other stuff.
The most likely reason for the crash would be memory access violation at
a->coll_box[0]

[font="Arial"]when a->coll_box is empty.

If you just want to add a new set of Vector3 pointers to a->coll_box, you will need to do a 2-level push_back: one at std::vector <Vector3*> level, then at [/font][font="Arial"]std::vector[/font] < [font="Arial"]std::vector <Vector3* > > level.
Create a temporary variable A of type [/font][font="Arial"]std::vector <Vector3*>, push_back the new-ed Vector3 object pointers into variable A, and then finally push_back A into a->coll_box.[/font]

@fisyher: Yes, that was it. Thank you very much :lol:
NEW QUESTION:

Hi, like I said above, my sprite class has a 2D vector like this:
std::vector < std::vector <Vector3*> > coll_box;

I am not sure how to properly delete the objects that those pointers point to, and then clear the vector so it is empty.

Should I go trough the vector elements and delete them, and then call vector::clear (twice )?
Is that the right approach?
[color=#1C2837][size=2]Does your vector<Vector *> need to be a pointer?
[color=#1C2837][size=2]

[color=#1C2837][size=2]Wouldn't it be a lot easier to just have [color=#1C2837][size=2]std::vector < std::vector <Vector3> > coll_box;
[color=#1C2837][size=2]

[color=#1C2837][size=2]It would save you from the "new" and "delete" pain.
[color=#1C2837][size=2]

[color="#1c2837"]Cheers!
Do you need to use pointers? Can you store them by value? That would simplify everything, and probably speed up your program too.

Should I go trough the vector elements and delete them...
[/quote]
If you're allocating them with new, yes you must delete them.

... and then call vector::clear (twice )?
[/quote]
If you clear the outer vector, you are removing all the inner vectors, so you don't need to manually clear them. You shouldn't need to clear anything twice.

Do you need to use pointers? Can you store them by value? That would simplify everything, and probably speed up your program too.

Should I go trough the vector elements and delete them...

If you're allocating them with new, yes you must delete them.

... and then call vector::clear (twice )?
[/quote]
If you clear the outer vector, you are removing all the inner vectors, so you don't need to manually clear them. You shouldn't need to clear anything twice.
[/quote]

[offtopic]
If you forget to call the delete the OS will still clean it up though after the process has terminated, this doesn't help with your memory situation while running your app though.
[/offtopic]


You shouldn't use clear any way after you called delete on your vectors use the swap trick to set your vectors back to using no memory and let the old one scope out in the temporary var you just created.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement