Passing pointers to functions

Started by
4 comments, last by jLeslie 23 years, 11 months ago
I''m looking for someone with a good knowledge of pointers to glance over some source code I have and tell me what I''m doing wrong.
Advertisement
Post it, baby!

And make sure to put the code in special blocks (before code, type "code" in brackets, and after code type "/code" in brackets).
Here it goes

If this doesn''t look right sorry, its my first time using this board

MoveModels is called from my main game loop, and I pass it a pointer to my ZoneObject struct, which holds a list of every model and its related data in the game.

The problem is with my FindFrame function. pFrame is going out of scope, (I believe that''s what its called ), when the function returns. Or rather, pFrame is still NULL when the function returns even though it does assign it to pMeshObject during the function call, I''ve checked with a debugger.

{code}
HRESULT MoveModels(ZoneObject* pZoneObject)
{

AnimationInfo* pAnimInfo;
MeshObject* pMeshObject;
pAnimInfo = new AnimationInfo;
pMeshObject = new MeshObject;

for (int i = 0;iObjectCount;i++)
{
if (pZoneObject->ZoneModel->pAnimInfo->CurrentAnimation != NULL)
{
pAnimInfo = pZoneObject->ZoneModel->pAnimInfo;<br> pMeshObject = pZoneObject->ZoneModel->pMeshObject;<br><br> if(pAnimInfo->IsBeingAnimated == TRUE)<br> {<br> ///The frame calculations aren''t right yet, but I''ll fix that later.<br> pAnimInfo->FrameNum = DWORD(((GetTickCount() * 0.001) - pAnimInfo->StartTime));<br> PlayAnimation(pAnimInfo, pMeshObject);<br> }<br> else<br> {<br> pAnimInfo->IsBeingAnimated = TRUE;<br> ///The frame calculations aren''t right yet, but I''ll fix that later.<br> pAnimInfo->StartTime = DWORD((GetTickCount() * 0.001));///Fill in timer function<br> pAnimInfo->FrameNum = 0;<br> PlayAnimation(pAnimInfo, pMeshObject);<br> }<br><br> }<br><br> }<br><br> return S_OK;<br>}<br><br>void PlayAnimation(AnimationInfo* pAnimInfo, MeshObject* pMeshObject)<br>{<br> AnimationSetObject* pAnimationSet;<br> Animation* pAnimation;<br><br> pAnimationSet = new AnimationSetObject;<br> pAnimation = new Animation;<br><br><br> if (pAnimInfo->CurrentAnimation != "ReturnToNorm")<br> {<br> LoadAnimation(pAnimInfo->CurrentAnimation, pAnimationSet);<br><br> if (pAnimationSet->Animation != NULL)<br> {<br> pAnimation = pAnimationSet->Animation; <br> ParseThroughAnimations(pAnimation, pMeshObject, pAnimInfo);<br> <br> }<br> }<br><br>}<br><br>void ParseThroughAnimations(Animation* pAnimation, MeshObject* pMeshObject, AnimationInfo* pAnimInfo)<br>{<br> MeshObject* pFrame;<br> AnimationKey* pCurrentAnimKey;<br> AnimationKey* pLastAnimKey;<br> D3DMATRIX* pTempMatrix;<br><br> pTempMatrix = new D3DMATRIX;<br> pCurrentAnimKey = new AnimationKey;<br> pLastAnimKey = new AnimationKey;<br> pFrame = new MeshObject;<br><br> pFrame = NULL;<br> pCurrentAnimKey = NULL;<br> pLastAnimKey = NULL;<br><br> FindFrame(pAnimation, pMeshObject, pFrame);<br><br>}<br><br><br>void FindFrame(Animation* pAnimation, MeshObject* pMeshObject, MeshObject* pFrame)<br>{<br> <br> if (strcmp(pMeshObject->ObjectName, pAnimation->Name) == 0)<br> {<br> pFrame = pMeshObject;<br> }<br> else<br> {<br> if (pMeshObject->Child == NULL && pMeshObject->Sibling == NULL)<br> pFrame = NULL;<br> if (pMeshObject->Child != NULL)<br> FindFrame(pAnimation, pMeshObject->Child, pFrame);<br> if (pMeshObject->Sibling != NULL)<br> FindFrame(pAnimation, pMeshObject->Sibling, pFrame);<br> }<br><br>}<br>{/code} </i>
Well that doesn''t look right at all.
The code tags need to be in square brackets

You're passing the pointer by value, that's your problem. Just like
Function(int a, int b)
will take a copy of a and b to use internally,
Function(struct* ptr)
will take a copy of ptr and use/change that internally. With pointers, you will get access to the target data (whatever ptr points at) but the actual original pointer itself is safe from your meddling

Someone posted a similar question recently. The answer is to pass the pointer by reference (or its address, in C).

So make the function declaration:

void FindFrame(Animation* pAnimation, MeshObject* pMeshObject, MeshObject*& pFrame)

This isn't tested, and I rarely have to do this, but the idea is that you are now passing a reference to a pointer to a MeshObject, so the pFrame inside your function is exactly the same pointer as you passed to it, so any changes will persist beyond the scope of the function.

If you don't understand this, just try it and see if it works. Then come back and try to work it out later.

Btw, I am assuming all your other values are being passed ok: I haven't checked your program logic.

Edited by - Kylotan on 5/3/00 5:44:38 AM
Thanks for the help, it worked perfectly.

This topic is closed to new replies.

Advertisement