LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2004, 05:52 PM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
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);
}
 
Old 08-28-2004, 07:05 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
Old 08-28-2004, 07:27 PM   #3
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
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
 
Old 08-29-2004, 06:07 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
Old 08-29-2004, 09:55 AM   #5
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
seg fault on fedora only dh0104 Programming 3 03-26-2005 12:57 PM
RH 7.2 crash...getting seg fault when using ls sakima Red Hat 1 10-11-2004 09:14 AM
C seg fault drigz Programming 5 10-01-2004 03:35 PM
xmms - seg fault :( :( xconspirisist Linux - Software 2 02-07-2004 05:38 PM
gnome_error_dialog seg fault?? Castro Programming 3 06-11-2003 01:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration