LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++: seg fault when trying to resize a vector of strings (https://www.linuxquestions.org/questions/programming-9/c-seg-fault-when-trying-to-resize-a-vector-of-strings-223662/)

R00ts 08-28-2004 05:52 PM

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);
}


kev82 08-28-2004 07:05 PM

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.

R00ts 08-28-2004 07:27 PM

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 :D

kev82 08-29-2004 06:07 AM

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.

R00ts 08-29-2004 09:55 AM

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. :)


All times are GMT -5. The time now is 05:38 AM.