Jump to content
  • Advertisement
Sign in to follow this  
Telastyn

[STL] Why does this not work again?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main(){

string                  st="foo";
vector<string>          x;

x.reserve(1);
x[0]=st;
cout << x[0] << "\n";
}

cores with a segmentation fault on the assignment, 3 levels deep in std::string calls. I do this same sort of thing with associative containers without any problem. Even doing this after pushing something onto x works fine. I thought it might just need the reserve() but no luck. What little boneheaded thing am I forgetting or ignorant of?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
To add something to a vector don't you need to call vector::push_back()?

Share this post


Link to post
Share on other sites
Because reserve() and resize() play different roles. After reserve(), you do have memory to hold the string object, but it hasn't been initalized. The assignment operator does assume that its target is a valid object. Since you used reserve() and not resize(), which would have actually created a string object in the allocated memory (and set the size of the vector appropriately), the assignment operator fails to work and you get a segmentation fault (probably related to the char* that a string object is supposed to contain).

Share this post


Link to post
Share on other sites
Heh. Fair enough. So why does the same thing, albeit with int's work:

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main(){


vector<int> x;
int st=42;

x.reserve(1);
x[0]=st;
cout << x[0] << "\n";


}



just one of those happy coincidences that it actually does work, since it should die like the string version?

Share this post


Link to post
Share on other sites
I wouldn't rely on that. you are most likely going out of bounds.
cout << x.at(0) << "\n";

would changing it to that cause it to exit?

a vector might treat an int differently because storing a bunch of pointers to int would be pretty wasteful. just because it doesn't crash doesn't mean it is correct :)

Share this post


Link to post
Share on other sites
Should I read all of the explanations here? And what does a Standard Template Library do? What is it? It is obviously a library of standard templates... but what is it really.

Share this post


Link to post
Share on other sites
std::vector::reserve preallocates a chunk of uninitialized memory, enough memory for specified number of elements, The advantage of this function is that if optimal code is a necessity and the user can determine the number of elements that will be required, the user can reserve the memory in advance, and thus prevent a possible reallocation of memory and copying of vector data.

So basically in your case the vector's capacity is one but the size is still zero (you can check this with std::vector::capacity). You need to initialize the element via push_back/front which basically boils down to an in-place copy construction on the uninitialized element.

[Edited by - snk_kid on June 11, 2005 6:55:36 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by nprz
I wouldn't rely on that. you are most likely going out of bounds.
cout << x.at(0) << "\n";

would changing it to that cause it to exit?

a vector might treat an int differently because storing a bunch of pointers to int would be pretty wasteful. just because it doesn't crash doesn't mean it is correct :)


Ah, yes, the change does blow it up.

It was just confusing, since I tried with ints to see if it was a problem with string use or with vector use. Since the ints worked, I spent a bunch of time trying to figure out what was wrong with elementary string use...

Share this post


Link to post
Share on other sites
Quote:
Original post by fuchu
Should I read all of the explanations here? And what does a Standard Template Library do? What is it? It is obviously a library of standard templates... but what is it really.


It is a lot of data structures that make your life easier with C++, so you don't have to reinvent the wheel for basic stuff. It also has a lot of functions such as sorting.
It is just a template so that it will work with different types of data: int, float, string, user defined classes.

If you know a lot about data structures, then STL is easy to understand, otherwise that might be something to study first.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!