   Pointers vs. The Object Itself
  Hi all,I''m trying to optimize (and fix several bugs and memory leaks) in my engine.  I have written an entire very beautiful engine in DirectX, but I have some questions about C++.  My knowledge of it is only "enough to get by", so here goes:For all of my classes, I simply create objects with them, not pointers to them.  For instance, I might have a ship class:class Ship(){    // Member variables    D3DXVECTOR3 position;    int whatever;    // Member functions    void LoadModel();    void Render();    void Unload();};And then use it in my main .cpp file like this:Ship falcon;falcon.LoadModel();...falcon.Render();...falcon.Unload();My question is, would it be easier/faster/more reliable/whatever to use the object as a pointer, like this:Ship falcon = new Ship();falcon->LoadModel();...falcon->Render();...falcon->Unload();delete falcon;Or does either way have the same effect on memory allocation or speed?  Which way is better?Thanks for any help you can give me.   Ryan BuhrReactor Interactive, LLC.rbuhr@reactorinteractive.net
  creating objects on the stack is faster i think ;  Now, I know it''s silly after coding an entire engine....but I don''t know what that means.  Does that mean use pointers, or use the dot operator?   Ryan BuhrReactor Interactive, LLC.rbuhr@reactorinteractive.net;  well, the advandage of using pointers is that it places the objects on the heap.  your computer's memory is essentially devided conceptually between heap and stack.  stack tends to be faster but you don't get much of it (around 2MB for typical applications i think).  Heap is essentially unlimited up to the total available RAM and hard drive space (though if you start using HD space, "swap space" your app will chunk out and run like it's a dying mule).  So having things on the Heap is nice b/c you end up with more space.  You'll obviously only be able to do dynamic memory with Heap variables (aka pointers).  for instance you really really don't want to have a std::vector of bigger objects because std::vector can memcpy your objects around frequently in memory when it changes size.  bad stuff that can slow things down tremendously.  it's far better to have a std::vector of pointers to your objects instead.  I also don't see how you're doing any kind of dynamic data structure like a linked list or anything without pointers to your objects.  maybe you don't need it yet but you certainly will.my general object container is something like:std::vector<GameObject*> gameObjects;i have an init function that looks something like:void initGame() {    for (int i = 0; i < numObjectsInLevelFile; ++i)   gameObjects.push_back( new GameObject(fileStream) );}then you later iterate through that object list in several different places for different reasons:physics uses the list for collision detection & physics updatesscenemanager picks from the list to select visible objectsrender uses the pruned list to draw visible objectsetc.I dunno, I don't really see a memory efficient way to write a game without a list of pointers to game objects or some other form of data structure of pointers.  I'm curious as to how you've gotten a game engine running that doesn't put game objects on the heap in some kind of dynamic memory store.  My sense is that you are loading every model every frame of your game then drawing it and then unloading it.  that seems HUGELY wasteful to me.  Normally you don't want to load things more than once, especially something as big as a model.  Maybe i'm just misinterpreting your description though.-me   [edited by - Palidine on February 17, 2004 2:58:12 PM];quote:Original post by HanSoL0  Now, I know it''s silly after coding an entire engine....but I don''t know what that means.  Does that mean use pointers, or use the dot operator?I''m just curious:  What does your "engine" do or contain exactly? ;  Once the object is created it is just as fast to access it either way.When you use new() to get a pointer you are creating the object on the "heap". This is a good idea if your object is very large, or you are making a lot of objects, or you don''t know how many objects you need until runtime. Otherwise it''s fine to declare them as a variable as you are already doing, with the advantage that you don''t have to worry about freeing them later. ;  Yeah, I don''t have any sort of memory manager, and I know I''m paying for it now.  This is exactly what I''m trying to impliment now.  No, I don''t have any linked lists ANYWHERE in my engine.  :\  For all the starfighters in my game, I literally just make an array of Ship objects, and load and render and update them in loops.  Same goes for the infinite asteroid field and all instances of ammo (lasers and missiles).  Needless to say, the only machine that will run my game at 45fps is a P4 2.2Ghz or higher with a GeForce 3 or higher, and around 256MB of RAM or higher.  Anything else either doesn''t run the game at all, or gets a framerate lower than 5fps.   Ryan BuhrReactor Interactive, LLC.rbuhr@reactorinteractive.net;  Well, my engine (maybe it's not truely an engine?) can load in .x files, texture, light, and render them.  It can do billboards and particles (and now that I think about it, the particles are a linked list), and I can do some vertex/pixel shaders like bump mapping and membrane rendering.  I've also got a fully 3D positional sound engine (Doppler, volume rolloff, etc.) with an MP3 player for music.  The input portion handles mouse, keyboard and joystick support with Force Feedback.In my game, you can fly one of 20 different starfighters against a few AI-controlled NPC's, shoot them down with either laser cannons or homing missiles, and they blow up with nice explosions and shockwaves.It has come along nicely, but my performance is crap.  And I did all this with only a passing knowledge of C++.  I have no idea how all the memory stuff works.You can check out my website for screenshots if you wish.   Ryan BuhrReactor Interactive, LLC.rbuhr@reactorinteractive.net [edited by - HanSol0 on February 17, 2004 3:10:39 PM];quote:Original post by HanSoL0  Yeah, I don''t have any sort of memory manager, and I know I''m paying for it now.  This is exactly what I''m trying to impliment now.  No, I don''t have any linked lists ANYWHERE in my engine.  :\  For all the starfighters in my game, I literally just make an array of Ship objects, and load and render and update them in loops.  Same goes for the infinite asteroid field and all instances of ammo (lasers and missiles).  Needless to say, the only machine that will run my game at 45fps is a P4 2.2Ghz or higher with a GeForce 3 or higher, and around 256MB of RAM or higher.  Anything else either doesn''t run the game at all, or gets a framerate lower than 5fps.  Have you profiled your code and found the bottlenecks? --Dave Mikesell Software;  I would like to, but I don''t know the best way of going about this.  And to ask how to do this, would probably be way too broad a question.  Any online resources you know of that could point me in the right direction?   Ryan BuhrReactor Interactive, LLC.rbuhr@reactorinteractive.net