Public Group

# vector, pointers and moving around

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

## 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 on other sites
You're making it way too hard on yourself.

Look into std::rotate.

##### Share on other sites
Did you intend to have a vector containing nothing but SDL Freed Surfaces?

##### Share on other sites
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 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 leftstd::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 rightstd::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.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 10
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632983
• Total Posts
3009707
• ### Who's Online (See full list)

There are no registered users currently online

×