Pointer to pointer

Started by
17 comments, last by MagTDK 21 years, 8 months ago
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]
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);
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

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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]
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]
<a href="http://www.purplenose.com>purplenose.com

I found the error.

Thanks for your help.
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]

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

So many guys have been trying to help u man, u say u found the error and u don''t wanna tell ?
"...and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces."----------Scott Meyers, "Effective C++"
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]
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.
If at first you don't succeed, redefine success.

This topic is closed to new replies.

Advertisement