Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MagTDK

Pointer to pointer

This topic is 5794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having a problem with pointers to pointers. I have several functions like so:
    
void SomeFunction();
{
byte *pData;
ProcessData (&pData);
// code that's not important for this question.

delete [] pData; pData = NULL; // delete pointer that was created in the convertdata function.

}

void ProcessData (byte **pData)
{
// code that's not important for this question

ConvertData (pData);
}


void ConvertData (byte **pData)
{

// code that's not important for this question

*pData = new byte[1000] // allocate memory

// code that fills what pData is pointing to

}
    
Problem is when I run the program I get an unhandled exception error. If I combine the code in the convertdata function with the processdata function, therefor eliminating the convertdata function, the program runs with no error. So I take it my problem has to do with passing the pointer to the 2nd function (convertdata). Note: I'd like to keep these two functions seperate as each function is quite large. So my question is how do I pass a pointer through two functions rather than one? [edited by - MagTDK on August 12, 2002 5:30:46 AM]

Share this post


Link to post
Share on other sites
Advertisement
I think it''s

*pData = new byte[1000];

You''ll have to change it to

*pData = new byte*[1000];

Oh and why do you do this

byte *pData;
ProcessData (&pData);

This may cause problems try this

byte **pData;
ProcessData (pData);

Share this post


Link to post
Share on other sites
Your code looks fine to me, although I can''t see why you''d want to pass a pointer to a pointer (instead of just a pointer). The error is probably in the code you omitted. Have you actually checked which line the exception occurs?

____________________________________________________________
www.elf-stone.com

Share this post


Link to post
Share on other sites
Yeah, it occurs when I try to delete the pointer:
delete [] pData; pData = NULL;

I checked the address in the debugger and it appears to be correct.

I just can't see why it would work if I copy everything from the convertdata function into the processdata when it's basically the same thing except for the call to the convertdata function.

quote:

Your code looks fine to me, although I can't see why you'd want to pass a pointer to a pointer (instead of just a pointer).



Don't I need the pointer to pointer when allocating memory for a pointer that was created outside of this function?

[edited by - MagTDK on August 12, 2002 6:20:17 AM]

Share this post


Link to post
Share on other sites
Change you function definitions to:

  
void ProcessData (byte *&pData)
void ConvertData (byte *&pData)



and things shoud be okay.

[edited by - carrot on August 12, 2002 6:33:01 AM]

Share this post


Link to post
Share on other sites
quote:
Don't I need the pointer to pointer when allocating memory for a pointer that was created outside of this function?

no.

It sounds like you've deleted the pointer when it hasn't been allocated. The access violation probably occurs because you try to delete an invalid pointer.

You should set your pointer to NULL when you initialise it so that you can tell if it's valid later on. You can then check the validity of the pointer before you attempt to delete it.

ie:

byte *pData=NULL;
ProcessData (&pData);// code that's not important for this question.
if (pData!=NULL)
{
delete [] pData;
pData = NULL;
}




____________________________________________________________
www.elf-stone.com

[edited by - benjamin bunny on August 12, 2002 6:44:05 AM]

Share this post


Link to post
Share on other sites
So many guys have been trying to help u man, u say u found the error and u don''t wanna tell ?

Share this post


Link to post
Share on other sites
void SomeFunction();
{
byte *pData;
ProcessData (&pData);
// code that's not important for this question.
delete [] pData;

This line is redundant:
pData = NULL; // delete pointer that was created in the convertdata function.
}

There's nothing technically wrong with this, but you should prefer to pass by reference here. Make the signature

void ProcessData(byte *&pData)

void ProcessData (byte **pData)
{
// code that's not important for this question

Whatever you've omitted here might be relevant. You can get a problem with deleting data when you have trashed the heap somewhere along the way.

ConvertData (pData);
}

Again, prefer pass-by-reference here:
void ConvertData (byte **pData)
{

// code that's not important for this question
*pData = new byte[1000] // allocate memory
// code that fills what pData is pointing to

Another location where you might be trashing the heap.
}

quote:

So I take it my problem has to do with passing the pointer to the 2nd function (convertdata).

You can confirm that in your debugger.

[edited by - SabreMan on August 12, 2002 6:55:43 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by benjamin bunny
It sounds like you''ve deleted the pointer when it hasn''t been allocated. The access violation probably occurs because you try to delete an invalid pointer.



Correct.

quote:

You should set your pointer to NULL when you initialise it so that you can tell if it''s valid later on. You can then check the validity of the pointer before you attempt to delete it.

ie:

byte *pData=NULL;
ProcessData (&pData);// code that''s not important for this question.
if (pData!=NULL)
{
delete [] pData;
pData = NULL;
}




Wrong. You can delete null pointers. You set pointers to null to make sure that delete knows not to, well, delete it. The following code will work just fine:


int main()
{
int *ptr = 0;
delete ptr;
return 0;
}


It''s when you try to delete an invalid pointer that is when you get problems. Null pointers are perfectly valid.

Death of one is a tragedy, death of a million is just a statistic.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!