Click to See Complete Forum and Search --> : me not understanding pointer's


polus
January 3rd, 2006, 11:47 AM
// call function
BYTE *pData;
Func( pData );
// pData no null.


// Function
Func( BYTE *pData )
{
pData = new BYTE[some_data_length];
memcpy( pData, some_data, some_data_length );
}


Why after the function returns is pData nullified?

The pointer is passed in fine, and the memory is allocated OK.

I know its elementary, but I seem to missing something obvious...

Thanks.

NMTop40
January 3rd, 2006, 11:52 AM
because pData is a pointer that is local to your function.

When you call new in your function this is for the local copy only.

Given that this is C++ code (you use new) then use vector (or basic_string).

But if you have to use pointers, then

Func( BYTE * & pData )

will fix your problem.

polus
January 3rd, 2006, 11:54 AM
Thanks. I dont know whats wrong with me today - I seem to have forgotten everything over the xmas break! :(

cilu
January 3rd, 2006, 12:40 PM
See this FAQ.