Advertisement

Problem using vector in linux.

Started by July 16, 2004 10:55 AM
14 comments, last by bobstevens 20 years, 6 months ago
I have already inspected the code in a debugger and the values are correct. They can be each be in the range of 0-60 and they are crashing when they are 29 and 1. However if I replace nWorldGrid[j] with 0, the first element, it still crashes.

This code is drawing a tiled map, which displays perfectly on windows. Unless VC++'s implementation of STL allows for more errors then g++'s, while still doing everything exactly how I wanted it done, I don't see where the bug could be.
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
Just checking up on this one though...

You aren't by any chance, adding or removing elements from a vector whilst holding iterators to it are you?

Vector is not safe for you to hold iterators while modifying its size. This will break equally regardless of implementation, but the exact size at which it will break may not be the same.

Mark
Advertisement
Quote:
Original post by griffenjam
strace prints a ton of crap that I think is internal SDL stuff, but I don't see anything that would give a clue as to what is crashing.


According to my bits/stl_vector.hpp header, the only data members are _M_start, _M_finish and _M_end_of_storage, which are pointers to the beginning, end, and memory block end of the vector. That's all the information a vector holds (it is a thin wrapper around a dynamically allocated array, after all).

That should help you debug. Check their values, see if you've overwritten them somehow.
"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
Don't use the [] operator on a vector. Use vImage.at(index) in place of vImage[index]. at() will throw an exception which can be examined in the debugger. The [] operator in vector is for people who are porting c programs to stl/c++
Quote:
Original post by markr
Just checking up on this one though...

You aren't by any chance, adding or removing elements from a vector whilst holding iterators to it are you?

Vector is not safe for you to hold iterators while modifying its size. This will break equally regardless of implementation, but the exact size at which it will break may not be the same.

Mark
Then you can do
iter = some_list.begin();  while(iter != some_list.end())  {    if(something())    {      //okay, continue loop      iter++;    }    else    {      //kill current       iter = some_list.erase(iter);    }  }
--monkey
strace is almost never useful for Linux debugging. Use gdb to examine the state of the program when it crashes. Compile without optimizations. Run it under valgrind to see if you're overwriting the stack somewhere or there's another memory issue. Chances are it's not the vector itself.

It's often very prudent to use C I/O in C++, to whoever asked why.

[Edit] Eek, didn't mean to raise the semi-dead, sorry.

This topic is closed to new replies.

Advertisement