Re: pointer, reference problem with a DLL and MFC
Claudia,
I believe I understand your query. You are trying to simulate the behavior of another DLL, and
therefore you cannot change the interface. The third parameter is a char * that is filled on output
from the function, and you wish to know how to handle the memory that is allocated.
Yes, you should allocate the array in memory that will not be destroyed when the function
returns. A stack variable would be the wrong choice because it does not persist. Allocating on the
heap may be the right choice, because it will persist beyond the return. Whether or not the caller
should delete the allocated memory is a question of the "contract" between the caller and callee.
It is quite common that the caller is responsible for deleting the allocation when memory is
returned in an "out" parameter. However, other function may be returning pointers to internal
buffers in static or global memory, or that was allocated in a way other than "new". The memory
release must match the allocation: new <-> delete, malloc <-> free, HeapAlloc <-> HeapFree, etc.
You need to know what the "contract" is for the caller.
In your first post, I observed that you called:
memset( &pOutput, 0, sizeof(pOutput) );
This is a incorrect usage, as you have passed the address of the pointer when you intended to
pass the pointer itself (the address of the char array). It should have been:
memset( pOutput, 0, sizeof(pOutput) );
You also need to be careful (as others have observed) to understand the distinction between a
"char" and a "char[ ]". The first is a single character only, whereas the second can be a longer
string. Do not pass a "char &" when a "char *" is expected. Also do not pass a "&char" to get a
"char *", as you will be passing only the address of a single char when an array buffer is expected.
Scot
"Claudia Maier" <cm@NewsMicro.com> wrote in message news:eMioDr7xJHA.1432@TK2MSFTNGP02.phx.gbl...
Hello
short description
int RegRead( char * p_InPath,
char * p_InConfig,
char * p_OutValue);
I should simulate a DLL function like this.
2 IN Parameter
1 OUT Parameter
I can create here a
new char[256] .
For test I delete not the buffer
Or?
My Query is after then a reference, or?
Thanks
Greet Claudia