Archived

This topic is now archived and is closed to further replies.

Using pointers to data in STL objects

This topic is 5185 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello, I was wondering if you guys knew of a way to efficiently do something like this (without the catch that a vector is allowed to move its memory around behind your back, and does):
class B
{
static vector<A> all_As;
vector<A*> As_in_here;
};
class A
{
static vector<B> all_Bs;
vector<B*> Bs_in_here;
};
basically I would like to have a ''master'' vector of some object, and then, pass out pointers to the objects when asked for. Basically, I am setting up a mini database, assume class A is a server class, and B is a LiveConecertSHNFolder. I would like each server to have a pointer to the shows/concerts that reside on it. I would also like every show to have a list or servers it resides on. It seems that I am getting corupt data using vectors. I seem to remember an STL class that garentees to you that the objects are never moved around, so it is legal to get pointers to them for use elsewhere... I think what is happening is that I am creating objects in the vector, I get the pointer, the vector class does some shuffling on the data, I try to access the memory at the pointer, and its all goofed up. Although i get no errors. Normally when you try to read/write from bad memory, you get errors. Anyone know if this is truly a catch with the vector class or if I must be doing something wrong in my other code somewhere... Thanks! Dwiel

Share on other sites
I just did a some of debuging, and I have traced the memory corruption to be exactly when a new object is ''push_backed'' onto the vector. I have one object in the vector. I get the pointer to it. Its fine; I add another object to the vector. the pointer points to bad data now. any ideas?

thanks a lot!

Dwiel

Share on other sites
Read the vector docs at http://www.sgi.com/tech/stl. Specifically, look for stuff on "iterator invalidation" (footnote 5, at the very bottom of the vector page). Also look at the "reserve" function.

"Sneftel is correct, if rather vulgar." --Flarelocke

Share on other sites
quote:
Original post by Tazzel3D
I just did a some of debuging, and I have traced the memory corruption to be exactly when a new object is 'push_backed' onto the vector. I have one object in the vector. I get the pointer to it. Its fine; I add another object to the vector. the pointer points to bad data now. any ideas?

thanks a lot!

Dwiel

This may or may not work, but if you know how many elements you want in the vector, you can try:

vector v;
v.reserve(n);

where n is the number of elements desired. Once you exceed n then reallocation may occur.

EDIT: sneftel beat me.

[edited by - nervo on December 13, 2003 11:58:22 PM]

Share on other sites
quote:
Original post by Tazzel3D
I just did a some of debuging, and I have traced the memory corruption to be exactly when a new object is ''push_backed'' onto the vector. I have one object in the vector. I get the pointer to it. Its fine; I add another object to the vector. the pointer points to bad data now. any ideas?

thanks a lot!

Dwiel

Yes, a vector allocates memory for the data, and puts it in... you add another object, and it has to allocate NEW memory for it... so you cannot use it this way, a better way would be with a linked list (std::list), as that only allocates each item as required and never has to change the location of any single element. Another idea would be to only store pointers in your list, then even if the memory location of the list changes, the memory addresses that it points to remain consistant...

I don''t feel you know how to really use vectors properly . If you store the points to the objects that you allocate, at the end, you can simply traverse the list and delete all allocated memory, this way you don''t have to keep all the data in pointers, the list will store all the pointers that need to be deleted.

Share on other sites
yeah, I guess i was just hoping that it would work, because I didn't want to have to worry about the memory allocation.. not that its difficult, just hopped that by doing it this way I would be save some time... guess not

I guess I'll just keep a list of pointers to the memory... I thought that by doing it this way and handling the allocation my-self I would be over-complicating things, but luckily due to my OOP design, I shouldn't have to worry about it... I already have a 'wrapper' around it for adding/removing data.

Oh, and do you think it is safe enough to just put a function call in the delocator of the class that contains this vector, that will delete all of the memory I used? Or would I be better off implementing a quick pointer class that will create a show in the constructor and delete it in the destructor.... that just seems a little over-done to me...

what do ya think?

Thanks!

[EDIT] Just went ahead and made it so I had a vector of pointers to memory that I manage, and its working great!

Dwiel

[edited by - Tazzel3d on December 13, 2003 12:16:36 AM]