A list that does not disturb the order when pushed/popped?

Started by
20 comments, last by ApochPiQ 9 years, 10 months ago

@servant: I voted up your answer, because I like the way you put it down, and also because I agree with everything you wrote.

But...

IMO, before using STL, or smart pointers or any "advanced" c++ things, a beginner should understand perfectly what is a (raw) pointer, how it's used, why they exists, what is a memory leak and why/how they are related to the use of pointers.

I think everyone should go trough the process of using pointers and discover that somewhere, sometimes, your program is leaking memory. You should fire up your debugger and go figure.

I think this is the only way to learn. When you understand the power of pointers, then you can choose a (may better) approach, like not using new at all for example.

So my complains against "this new trend" is more about the fear that the the new generation of programmers will grow up without understanding the raw power of c/c++. Why using c++ in first place if you don't have the time/will to learn the hard stuff. There are plenty of other languages that will deal with pointers in a transparent way (c# is a good example).

Anyway, in this specific case, I would have used a placement new, over a pre-allocated memory block of 256 * sizeof(Player), maybe with a little care about memory alignment.

The array of 256 pointer to player is still the best option IMO for a few reason:

1) given the player ID, you have O(1) access to the player data

2) search within the array for null pointers is very cache friendly since a single pointer is only 4 or 8 bytew, so the whole array is 1 or 2 KB of memory

3) from a psychologically point of view, calling new when a new player enter the game, and calling delete when he leave, is something that feels right to me

When a player come, you have to initialize a player class. If you use new Player(), the constructor will take care of initialization; if you use an already existing instance of player, then you have to call some method like player[id].initialize() or player[id].whatever() just to setup the class.

I fell like new Player() is better.

The same goes for a player that log off, calling delete player[id] feels better to me than calling player[id].kill()

It's the same thing, just called with different names.

I agree that a std::vector of pointers is a non sense, that's why my solution involve a plain old array of pointers.

Advertisement

Let's reserve the philosophical debate over learning style (which is endless and therefore pointless) for other threads, and keep this on the subject of the poster's problem, please.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement