Sign in to follow this  
simon10k

pointers to objects

Recommended Posts

Reading another post in this forum just trigger a thought, why do people create pointers to objects when there is no obvious reason to do so. If there is please enlighten me.
int main()
{
    Game* game = new Game;
    game->run();
    delete game;
}

// Why not just do

int main()
{
    Game game;
    game.run(); //edit: oops
}


I see examples like this all the time, there are obviously no virtual functions involved so why do it. It makes code look ugly and your have to explicitly delete the object when you’re finished. [Edited by - simon10k on April 22, 2006 12:13:44 PM]

Share this post


Link to post
Share on other sites
actually that would be

Game game;
game.run();

why you would put the game or game engine as a pointer, i don't know, but i'll link to some other explanations that's popped up in the last hour.

http://www.gamedev.net/community/forums/viewreply.asp?ID=2572448

Share this post


Link to post
Share on other sites
If the game is large, then it will be always taking up that amount of stack space. If you want to reduce the amount of space taking up the stack you can put it on the heap.

But I'm not sure thats going to be a problem most of the time.

Share this post


Link to post
Share on other sites
Quote:

That's part of the reason I use pointers - another is that you have to use the default constructor if you make member objects of a class on the stack


Huh?

Have you ever tried:

Object object( arg1, arg2, arg3 );

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
If the game is large, then it will be always taking up that amount of stack space. If you want to reduce the amount of space taking up the stack you can put it on the heap.

But I'm not sure thats going to be a problem most of the time.

well speaking in terms of consoles (ie. Dreamcast, PS2, Xbox, GC), how is the RAM split between stack memory and heap memory? is there a split or the program just uses memory 'til it runs out?
(sorry for the hijack)

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Huh?

Have you ever tried:

Object object( arg1, arg2, arg3 );

Yes I have; it was a long time ago when I sucked even more at C++ and I could never get it to work. I suppose you'll also want to construct inside the owner-class's constructor via pointer if it uses the parameters of the owner-class constructor, but if what you just wrote works fine I feel pretty silly.

Anyway, it's reasons like this that I edit out my post and let smarter folks answer!

Share this post


Link to post
Share on other sites
Quote:
Original post by Frequency
Quote:
Original post by rip-off
Huh?

Have you ever tried:

Object object( arg1, arg2, arg3 );

Yes I have; it was a long time ago when I sucked even more at C++ and I could never get it to work. I suppose you'll also want to construct inside the owner-class's constructor via pointer if it uses the parameters of the owner-class constructor, but if what you just wrote works fine I feel pretty silly.

Anyway, it's reasons like this that I edit out my post and let smarter folks answer!


You can use initialiser lists for that...


class OwnerClass
{
Object member;

OwnerClass( int arg1, int arg2, int arg3 )
: member( arg1, arg2, arg3 )
{
}
};

Share this post


Link to post
Share on other sites
I would guess the stack and heap both use RAM. And the stack memory is released when the name goes out of scope. Correct me if I'm wrong, we dont want the blind leading the blind ;)

so


int main()
{
Game* p = new Game;
p->run();
delete p;

//The game memory has been freed, do something else
/* ... */
}

//...

int main()
{
Game p;
p.run();

//The game object still exists, so less memory to use
/* ... */
}



Also, does this answer my question why people might use the heap for this?

[Edited by - simon10k on April 22, 2006 12:17:40 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this