Skip to main content
gamedev.net
🔒 Locked

Passing a pointer

Started by gimp May 17, 2004 at 1:15 AM 2 replies 350+ views
Original Post
gimp
gimp
Sanity Check... void ParseString(const std::string& a_Data, CObject* a_Parent, size_t& a_CurrentOffset) { a_Parent = Something } Once the function exits the pointer passed in, a_Parent loses it''s value. It''s like it''s say an int being passed by val. ... but it''s a pointer. I thought it didn''t matter. Have I lost it? Does it always work like this? Have I just never passed a pointer in to a function before and it always does this? I''m a bit embarresed even asking this...
Chris Brodie
SiCrane
SiCrane
You passed a pointer by value, if you want the calling function''s CObject * to be modified by your ParseString() function, either pass the parameter by reference or pointer. i.e. either CObject *& or CObject **.
HFX
HFX
Yep It will happen every time. The problem is in your function you created data on the stack. When you left the function, that data was deallocated. Since you assigned your data to the pointer, upon leaving, your pointer points to no mans land. A way to solve this is to create a new object on the heap (use new) and delete it later. Also your a_parent needs to be dereferenced unless your assinging a point to it.
My name is my sig.
gimp
gimp
Thanks. Looks like I''ve never done this before. I thought that because a_Parent was being assigned on the heap that the pointer would be valid. Since the point was being passed by ref the address change on the stack insude the function was getting destroyed the callee remained unchanged.

I reordered the functions and use a return value to allow it to pass back up.

Many thanks,

Chris
Chris Brodie

Topic Locked

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

Sign in to reply to this topic.