Welcome, Guest. Please login or register.

Author Topic: A C++ Vectors question  (Read 3033 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
A C++ Vectors question
« on: August 27, 2007, 04:19:38 PM »
I'm writing a GP2X game in C++. I have created a class called "Enemy", and then created a vector of Enemy called "enemies". When an enemy is killed, the enemy is removed from the vector using "enemies.erase(enemies.begin()+i)". This works, but when the last enemy is killed (enemies becomes empty?) the game quits. Any ideas why this would happen?

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: A C++ Vectors question
« Reply #1 on: August 27, 2007, 04:53:04 PM »
Ok I coded around my problem with the game quitting. I just changed my for loops from 0 -> enemies.size()-1 to 1 -> enemies.size()-1. So enemies[0] never gets moved, shown or checked for collision. Because it can't be seen or destroyed, it remains in the vector, so all of the (visible) enemies can be destroyed and the game doesn't quit. I still want to understand this problem, but hey, it works now  :)  I will look in to lists when the time comes to optimize my game, but for now I want to get back to writing my game :D

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: A C++ Vectors question
« Reply #2 on: August 27, 2007, 07:14:16 PM »
Out of curiosity, are you sure a vector is the right container for this application?

If you can kill off any enemy at any time, this implies that you will be randomly removing entries. A vector is not the best candidate for this type of use. A list based container like a queue or deque might be better.

-edit-

:lol: just read your second post...

With regard to the bug, have you tried creating a test case where you simply create a vector, remove all the elements then destroy the vector? Perhaps there's a bug in the implementation leading to a double-free or some other such oddity. This is for the GP2x, right?

At the risk of sounding heretical, I'd not use the STL for game type applications unless I knew the system was a beast and the compiler optimal...
int p; // A
 

Offline mdwh2

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 565
    • Show only replies by mdwh2
Re: A C++ Vectors question
« Reply #3 on: September 02, 2007, 01:02:59 AM »
Quote
At the risk of sounding heretical, I'd not use the STL for game type applications unless I knew the system was a beast and the compiler optimal...
The STL seems to be controversial among some game developers, but it's not really clear why. Why should code which has been developed by many programmers, and used and tested by thousands of developers, be slower than something you quickly code up yourself?
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: A C++ Vectors question
« Reply #4 on: September 02, 2007, 11:55:49 AM »
@mdwh2

I'm not knocking the STL, it's very powerful and the containers and algorithms are usually highly optimised and I do use it.

However, I'm also realistic about things. The STL is a genereral purpose, template based library and is not the most efficient solution for everything. As with all things, it comes down to common sense. I'd never use a std::vector for a bitmask, I'd never use a std::string to hold an invarying string where a C style const char* is just as useful. I don't use C++ streams for basic IO (although if I need some complex tokenisation etc, then of course I would).

The reasons to use or not to use STL amongst game devlopers varies. Although I'm not a game developer, in my case, it comes down to many hours of examining the compiler output and profiling code. Wherever cycle-efficiency has been important I've managed to find faster solutions to just about everything it provides that I would otherwise use, but of course that is at the expense of generality.
int p; // A