Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.






Thought VS Reality and the Machine

Posted by cedifra, 27 March 2014 · 301 views

c++ sdl game problems solutions 2d retro puzzle adventure
What a night

I finished work late... so I knew this was gonna be a short night. Wanting to tweak my code a little before "going to sleep" I ran into a "intended algorithm versus actual implementation behaviors" kinda issue. And yes, it drove me nuts.

Looking at it now, I am rather embarrassed and I feel humbled by the fact, that I receive so much help online. So let's have a quick look at it:
//Remove Object
if (button2 == true)
{ 
    //go through objects and check collision
    for (std::vector<cOBJECT*>::size_type i = 0; i != GameObjects.size(); i++)
    {
        //Check for collision and delete object
        if (MouseRect(GameObjects[i]->getrect(), mx + offX, my + offY) == true)
        {
            //GameObjects[i]->~cOBJECT();
            delete GameObjects[i];
            GameObjects.erase(GameObjects.begin() + i);
        }
    }
} // if (button2 == true)
Some of you might be able to point it out straight away. Well I could not wrap my head around it for some reason.

So this is what I posted online:

1) Access violation reading location 0xFEEEFEEE.
It seems to somehow have an issues with me destroying the texture. If I take out the "delete ...." and replace it with the destructor of the object instead, it works fine.
2) Vector subscript out of range
So if I use the destructor to pass the first problem. I run into the next one. Now even if I use "GameObjects.erase(GameObjects.begin());" I get the same error.


I was not prepared for the mass of replies I received... but there was lots of friendly and helpful feedback. Here are two of the most helpful pieces:

1)

If you think carefully about what the operation you implemented does you will notice that when the i-th element matches you remove that element from the vector and the (i+1)-th element is moved to the i-th position, but at this point the end of the loop is reached and i is incremented, which means that you will not test the element that was in the (i+1) position originally (and is now in i-th position) and also that if the value of i is GameObjects.size() - 1 before the removal the variable i now has a value that is GameObjects.size()+1 and the loop won't terminate.


2)

You don't want to loop on i in the first place. You want to use an iterator and either advance it with the erasure when your action condition is true: (delete *it; it = GameObjects.erase(it);) or advance it with an increment when the action condition is false: ++it, but not both. Finally, change the for-condition to be it != GameObjets.end() and remove the increment step entirely (or better still, use a whileloop).

How could I not see this? Well first, I decided to use a simple

break;

This got me out of the loop and everything worked well. Not the nicest solution tho. So I decided to look at the solution suggested not to loop on i and.... finally....
//Remove Object
if (button2 == true)
{
	//go through objects and check collision
	for (std::vector<cOBJECT*>::iterator it = GameObjects.begin(); it != GameObjects.end();)
	{
	        //Check for collision and delete object
		if (MouseRect((*it)->getrect(), mx + offX, my + offY) == true)
		{
			// delete object, delete slot, pick up next slot
			delete *it;
			it = GameObjects.erase(it);
		}
		else
		{      // no action, move to next
                       ++it;
		}
	}
} // if (button2 == true)

Sometimes it is better to ask than to waste 3 hours trying to figure it out. In this case, it should have been obvious... but staring at code for too long... well you know. Here is a machine:

Attached Image



I will post some more screenshots as soon as I get some decent sprite sheets.

Did you run into something similar? If so, let me know, I am interested.

C




hehe, we all make dumb mistakes sometimes. Especially in C, C++, Javascript, PHP, Ruby, Lisp, etc... as the man said, there's no silver bullet :D

hehe, we all make dumb mistakes sometimes. Especially in C, C++, Javascript, PHP, Ruby, Lisp, etc... as the man said, there's no silver bullet biggrin.png

 

Yessir, more often than I would like. 

Random Album Image

Slooooowly getting closer

Recent Comments

Recent Entries

December 2014 »

S M T W T F S
 123456
78910111213
141516 17 181920
21222324252627
28293031   

Latest Visitors

PARTNERS