Skip to main content
gamedev.net
🔒 Locked

Quick Question about Pointers

Started by 1kevgriff 5 replies 550+ views
Original Post
1kevgriff
1kevgriff
At first, when I learned about pointers, I said to myself "When am I ever going to use these?" Well, now that I''ve been programming for a while, I think I know why pointers would be useful - but I don''t want to mess with the code I have written so far (at least 10 hours of work). So, I''ll ask a question here to see if what I''m thinking is right. Let''s say I create a object in one function that is used just to set up character profiles (for a game). Well, after that function is done running - the object is lost. Can I use a pointer (or *gulp* a series of pointers) to keep all of the class information on the freestore? Then when the program exits, have another function remove them from the freestore? Just curious. Thanks
djsteffey
djsteffey
is this what you mean
  

void main()
{
Object* myObject;
allocateMem(myObect);
// now i can do anything with myObject

myObject.doStuff();
deallocateMem();
}

void allocateMem(Object* myObject)
{
myObject = new Object;
}

void deallocateMem(Object* myObject)
{
delete myObject;
myObject = null;
}


if that is what you mean, then you can do that and it should be fine

"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Succinct
Succinct
here''s what i''m seeing happen

tProfile* CreateNewProfile( )
{
tProfile NewProfile;

return &NewProfile
}


that won''t work, cuz NewProfile is allocated on CreateNewProfile()''s stack, not the freestore, and so is destroyed when the function returns.

if i''m right, here''s what u''d need to do:
tProfile* CreateNewProfile( )
{
tProfile* NewProfile = new tProfile;

return NewProfile;
}

void KillProfile( tProfile* pProfile )
{
delete pProfile;
}


hope this was what you meant

//////////////////////////////

I have no name that you may call me. I am Succinct, so call me as such.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
-- Succinct(Don't listen to me)
1kevgriff
1kevgriff
Thanks for the responce. Got the exact answer I needed.
1kevgriff
1kevgriff
Maybe I spoke too soon on the last post. Here is what i write:

    

Mp3* Fade; // create object "Fade" with class "Mp3"

Fade = new Mp3; // put Fade on the freestore


// ... later in code


Mp3.Load("c:\\mp3\\fade.mp3"); // Run a function of Mp3

// Error recieved!



Why can't I use the class functions?

Edited by - Coaster Kev on November 21, 2000 3:32:16 PM
Claus Hansen Ries
Claus Hansen Ries
There exsists no Mp3 but a pointer to a Mp3 object.

Fade->Load("c:\\mp3\\fade.mp3");
Ries
karmalaa
karmalaa
quote:
Original post by ncsu121978

is this what you mean [...]

if that is what you mean, then you can do that and it should be fine




I don''t think it would work. It should be

    

void main()
{
Object *myObject;
allocateMem(&myObject);
myObject->doStuff();
deallocateMem(&myObject);
}

void allocateMem(Object **myObject)
{
*myObject = new Object;
}

void deallocateMem(Object **myObject)
{
delete *myObject;
*myObject = null;
}


You have to use TWO LEVELS of indirection. Or just use C++ references...



Bye,

Karmalaa

---
"Lifting shadows off a dream once broken
She can turn a drop of water into an ocean"
---[home page] [[email=karmalaa@inwind.it]e-mail[/email]]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.