C++ : Is using stl containers like this safe?

otherwise

Member
Nov 20, 2005
52
0
0
m_values is a std::map<int, Structure>

Code:
Structure& structure = m_values[KEY]; 
structure.setValue(whatever);

Basically, does stl gaurentee that the Structure& reference will always be valid, or does stl internally copy around members under certain circumstances?
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Of course that isn't safe you didn't check the return value. What structure points to will never change even if the map changes. Iterators on the other hand can be invalidated.

I don't know how you plan to use the above code but it most likely isn't safe. Looks like a memory error like waiting to happen if you plan on keeping structure around for any amount of time. Of course if you are using thread you should be locking and unlocking a mutex.
 

otherwise

Member
Nov 20, 2005
52
0
0
Check the return value of what? The structure is the return value of m_values[KEY], and will construct an object if the KEY doesn't exist.

I really need a "yes" or "no" answer to the underlying question, if assuming code is guarded properly, if the code is considered valid.

I guess what I'm really asking is:

"Given a reference to a stored object within a STL container, is it gaurenteed that said reference will be valid for the lifetime of the STL container?
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: otherwise
"Given a reference to a stored object within a STL container, is it gaurenteed that said reference will be valid for the lifetime of the STL container?

No. It is only guaranteed to be OK until your next non-const access to that container.

If you need it to be more persistent, use a map<int, Structure*> instead.

 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Not safe. Like degibson said, the map is going to shift around data all over the place, you effectively have a reference to one of the places in the map (While it isn't exactly like this, think of a giant array with a lot of spots filled with garbage and some spots filled with vaild data. Your reference points to one of the spots, not the data in the spot, if that makes sense)

Your best bet is to pass in *'s and then modify those values. Then you will pretty much always be safe regardless of what you do to the map. The only problem you run into is that now you have to worry about memory management.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: otherwise
Ok, ugh.

Do STL iterators have the same lifetime rules, or are these a bit smarter?

iterators are not guaranteed to be valid after a mutable method is called on the container.

there are probably some STL-safe smart pointers that you could use. try looking in the "boost" library.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: IHateMyJob2004
stl .... should be call nstl

non standard template library.

I hate that thing.

I used to hate it, too. If you just come to realize that STL won't ever do exactly what you want it to do -- that STL only does what it wants to do, then you'll be OK. Programmers can adapt to anything.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Just stop trying to mess with the underlying structure and you'll be fine. The STL is great at what it was designed to do, but the moment you try and use a map like an array, you've stepped where you shouldn't have. The fact that they give the map iterators is somewhat questionable IMO. Would you give your BTree iterators?
 
Sep 29, 2004
18,656
67
91
Originally posted by: degibson
Originally posted by: IHateMyJob2004
stl .... should be call nstl

non standard template library.

I hate that thing.

I used to hate it, too. If you just come to realize that STL won't ever do exactly what you want it to do -- that STL only does what it wants to do, then you'll be OK. Programmers can adapt to anything.

I've used it in the past. It's usable. The thing is, I've also used Java and java is about 100 times better than STL. Seriously, 100 times better.
 

Madwand1

Diamond Member
Jan 23, 2006
3,309
0
76
Originally posted by: otherwise
Basically, does stl gaurentee that the Structure& reference will always be valid, or does stl internally copy around members under certain circumstances?

"Always" is a long time, but I think that in a sense you probably mean, it should be safe.

http://www.sgi.com/tech/stl/Map.html

Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.

The original Stepanov paper is a bit clearer about references:

insert does not affect the validity of iterators and references to the container, and erase invalidates only the iterators and references to the erased elements.