[STL] Why does this not work again?

Started by
11 comments, last by snk_kid 18 years, 10 months ago

#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?
Advertisement
To add something to a vector don't you need to call vector::push_back()?
Change it to resize, not reserve.
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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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?
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 :)
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.
My friend wants to learn to program in C++. If he forgets BASIC right away, well, I am worried.
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]
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...
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.

This topic is closed to new replies.

Advertisement