Jump to content
  • Advertisement
Sign in to follow this  
caleb_yau

iterators and SIGSEGV

This topic is 3792 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <vector> #include <memory> #include <iostream> using namespace std; void program(vector<int*> & foo) { static int count = 0; if (count < 20){ int * pfoo = new int; *pfoo = 4; foo.push_back(&(*pfoo)); cout << *(foo[0]) << endl; cout << *(foo[1]) << endl; count++; } } int main() { vector<int*> foo; int first; first = 5; foo.push_back( & first); for(vector<int*>::iterator it = foo.begin(); it != foo.end() ; ++it) { program(foo); cout << *(foo[1]) << endl; cout << *(*it) << endl; } } The output is this: 5 4 4 Segmentation fault (core dumped) So it seems that the memory is allocated to the vector foo but my iterator can't access it for some reason. When I replace the iterator for loop with just integers (i.e. for (int i = 0, i < 20, i ++) cout <<*(foo) << endl;) the loop works fine. What is up with iterators?

Share this post


Link to post
Share on other sites
Advertisement
Please use [ source ] tags for indentation.

All current iterators can be invalidated when you insert into a vector. Inside your loop, you call program which calls push_back, inserting an element, invalidating your iterators. You then print the contents of *foo[1] and **it, the later of which uses an invalidate iterator, which in turn invokes undefined behavior (in this case, crashes). If you ran this code in a debugger, you should notice that the call stack points to the line of code in question [relevant article for you to read].

...on a blunt aside, your code appears to be a frankenstein nightmare, and I'm left wondering what the heck it is you're trying to do? You may want to elaborate on this? Please?

Share this post


Link to post
Share on other sites

void change_frame(AnimationList & Sprites, const SDL_Event & event)
{
for (AnimationList::iterator it = Sprites.begin(); it != Sprites.end() ; ++it) {
switch ((*it)->show_char()) {
case type_enemy:
fire_shot(Sprites, (*it)->enemy_animation(event));
break;
case type_shot:
(*it)->shot_animation();
break;
}
}
}



void fire_shot(AnimationList & Shot, int direction)
{
if (direction > 0) {
animation * pfoo = new animation;
pfoo->initialize_shot(direction);
Shot.push_back(&(*pfoo));
}
}




So the idea is that I want to call the change_frame function to change various members of my animation objects based on inputs i get from the mouse and keyboard. AnimationList is a typedef for vector<animation*> and at all times I store an arbitrary amount of Sprites (animation objects) in the vector. The reason I need the fire_shot code is to create a new 'shot' Sprite so that I can have a bullet being fired by one of my other objects.

This is the largest project I have ever tried to work on. This is also the first time i'm using containers (even classes for that matter). So any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!