C++: seg fault when trying to resize a vector of strings
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C++: seg fault when trying to resize a vector of strings
I'm stumped as to what the problem is. I can resize a two dimensional vector of ints just fine, but in the same function if I try to resize a vector of strings I always get a seg fault. Does anyone know what the problem is? Can you not resize string vectors or something? Anyways here is the code.
Code:
void MapData::LoadData(string mapname) {
tile_vector.clear();
for (unsigned int r = 0; r < map_vector.size(); r++)
map_vector[r].clear();
map_vector.clear();
// The following is temporary code
if (mapname == "testing") {
num_tile_rows = TILE_ROWS * 2;
num_tile_cols = TILE_COLS * 4;
col_pos = 2 * TILE_COLS + 4;
row_pos = TILE_ROWS;
num_tiles = 15; // 15 unique tiles for this test
//tile_vector.resize(num_tiles); For some weird reason, this causes a seg fault
tile_vector.push_back("tile/bones_01.png");
tile_vector.push_back("tile/clay_01.png");
tile_vector.push_back("tile/desert_01.png");
tile_vector.push_back("tile/desert_02.png");
tile_vector.push_back("tile/desert_03.png");
tile_vector.push_back("tile/dirt_01.png");
tile_vector.push_back("tile/forest_01.png");
tile_vector.push_back("tile/path_01.png");
tile_vector.push_back("tile/path_02.png");
tile_vector.push_back("tile/road_01.png");
tile_vector.push_back("tile/road_02.png");
tile_vector.push_back("tile/rock_01.png");
tile_vector.push_back("tile/rock_02.png");
tile_vector.push_back("tile/wall_01.png");
tile_vector.push_back("tile/water_01.png");
for (unsigned int i = 0; i < tile_vector.size(); i++)
VideoManager.LoadImage(tile_vector[i]); // Load all those images in the cache
// Resize our map_vector
map_vector.resize(num_tile_rows);
for (int r = 0; r < num_tile_rows; r++)
map_vector[r].resize(num_tile_cols);
}
the segfault is somewhere in LoadImage (confirm with gdb) because you are calling it with 15 empty strings. the function call you want is not resize(), its reserve().
from sgi's stl manual void resize(n, t = T())
Inserts or erases elements at the end such that the size becomes n.
void reserve(size_type n)
If n is less than or equal to capacity(), this call has no effect. Otherwise, it is a request for allocation of additional memory. If the request is successful, then capacity() is greater than or equal to n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
Ok, it makes sense that the seg fault is occuring in Loadimage, thanks for the help there. But I don't think reserve is what I'm looking for (I took a look at the C++ vector functions before posting here). The thing is, I'll be calling this function tens, maybe hundreds of times and if I "reserve" additional space for each new tile set I bring in, I'm going to be reserving a lot of memory that won't ever be used.
I don't really understand why its loading empty strings, because I thought after resizing the vector and then pushing all of those non-empty strings to fill the entire vector up I'd be over-writing all the empty strings. Hmm, well I know enough now to fix the problem myself though. Thanks kev
by R00ts I don't really understand why its loading empty strings, because I thought after resizing the vector and then pushing all of those non-empty strings to fill the entire vector up I'd be over-writing all the empty strings.
read the definition of resize() again. what your code does is empty the vector, push 15 empty strings onto it, push 15 other strings onto it resulting in 30 strings, you can check this by dumping the vector to stdout.
But I don't think reserve is what I'm looking for (I took a look at the C++ vector functions before posting here). The thing is, I'll be calling this function tens, maybe hundreds of times and if I "reserve" additional space for each new tile set I bring in, I'm going to be reserving a lot of memory that won't ever be used.
read the definition of reserve() again, it only allocates more space if you request more than the vector's current capacity so the vector will only ever be the size of your biggest call to reserve.
it looks to me as though your not very familiar with the stl container classes, i seem to remember that "thinking in c++" by bruce eckel has a some good chapters on the subject and its available online if you google for it.
was my quick opengl code any good? you never posted back on your msg board so i assume it worked and you understood it.
Ahh that makes sense now. Thanks. I'm familiar with the container classes. Its just been a while since I used them, and I never used them very extensively before.
Yeah I forgot to take a look at that code, sorry. I ran it just now and posted my comments on the Allacrost forums, thanks for reminding me.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.