Jump to content
  • Advertisement
Sign in to follow this  
theSecondt

vector, pointers and moving around

This topic is 4325 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

hey, i've made a std::vector<SDL_Surface*> of size 5. the vector contains, emm, 5 SDL_Surface*s :| now i'd like for the elements in the vector to move one place to the left(i-1). i've made a simple for-loop that _should_ move the elements to the left, but it does not work, instead, it crashes. my guess is that pointers are make my 'moving around' a little harder. this is what i have done:
  if(sprite.size()>= 5){ 
    for(short i= 1; i< 5; i++){
      SDL_FreeSurface( sprite[i-1] );
      sprite[i-1]= sprite;
    }
    SDL_FreeSurface(sprite.back());
    sprite.pop_back();
  }
  sprite.push_back(newSprite);



tnx!

Share this post


Link to post
Share on other sites
Advertisement
jpetrie: oh, lol. c++ really has everything!
NotAYakk: i don't know about the current code, but if i add sprite=NULL, it blits fine, but memleaks :s

Share this post


Link to post
Share on other sites
First of all, when you say "move to the left", what do you want to happen to the zeroth (initial) element? If you want it to reappear at the end, then std::rotate is what you want. If you just want to get rid of it, then the operation you're really performing is what most people would call "removing the zeroth element", and you can do that by simply using the .erase() member of the vector:


SDL_FreeSurface(sprite[0]);
sprite.erase(sprite.begin());


Using std::rotate looks like this:


// rotate one step to the left
std::rotate(sprite.begin(), sprite.begin() + 1, sprite.end());


This function lives in the <algorithm> header.

To rotate right, note that rotating by the entire size of the vector leaves it untouched, and therefore rotating by N-1 to the left is equivalent to rotating 1 to the right:


// rotate one step to the right
std::rotate(sprite.begin(), sprite.end() - 1, sprite.end());





Second, you completely missed the point of what NotAYakk is saying. The code you've provided calls SDL_FreeSurface() on *every* surface. You should only free the surface that you're actually getting rid of, i.e. the one at index 0.

Also, your logic is a little frightening in terms of what it says about the design. Is it actually possible for the sprite.size() to be *more* than 5? If so, what are you going to do about the other elements? (Better to process all the ones that are there. Note that both std::vector.erase() and std::rotate() will do that, not caring about how many elements there are.) If not, why are you checking for that case? Better style would be something like:


assert(sprite.size() <= 5); // deliberately stop everything if that's not true
// so that we get told that our assumption is wrong and can fix it.
// BTW, instances of containers typically deserve a plural name, i.e. "sprites".
if (sprite.size() == 5) {
// We should not do this manually (see above), but here's how:
SDL_FreeSurface(sprite[0]);
for (int i = 1; i < 5; i++) { // Just use int. Really.
sprite[i-1] = sprite; // don't free the sprites we're moving!
}
sprite.pop_back();
}
sprite.push_back(newSprite);


Remember, we're only copying and moving pointer values around; the actual SDL_Surfaces are not going to get copied, only SDL_Surface*'s.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!