Returning a Pointer

  • Thread starter Thread starter Abubakar
  • Start date Start date
A

Abubakar

Hi,
its a c++ question. If I have the following code:

TCHAR * getsomestring()
{
TCHAR buff[2000];
// here I copy something in the buffer using wcscpy.

return buff;
}

Is this a valid code? I mean I see it as a buff allocated on the stack which
should be destroyed on the return of the function but I return it and some
other code will still have a pointer to it and try to read it. It works fine
but I wanted to clear the concept.

Thanks,

-Ab.
 
No, it's not a good idea to return a pointer to data like that. The
buff is destroyed and memory deallocated on exit from the function. If
the program works afterwards it's a fluke.
 
Hi Abubakar!
its a c++ question. If I have the following code:

TCHAR * getsomestring()
{
TCHAR buff[2000];
// here I copy something in the buffer using wcscpy.

return buff;
}

Is this a valid code? I mean I see it as a buff allocated on the stack which
should be destroyed on the return of the function but I return it and some
other code will still have a pointer to it and try to read it. It works fine
but I wanted to clear the concept.

It is a valid code (by the means: it will be compiled without error),
but it will couse some real trouble...

In general: It is not recomended to returns strings!
If you use C++, please use come string-class, then it is safe:

std::string getsomestring()
{
return "hello world";
}


If you have no string-class, then please use the common approch to let
the caller provide the buffer for the string:


BOOL getsomestring(LPTSTR szBuffer, SIZE_T nBufSize)
{
if ( (szBuffer == NULL) || (nBufSize <= 0) )
return FALSE;
TCHAR szString[] = TEXT("Hello wolrd");
if (_tcslen(szString) > nBufSize)
{
_tcsncpy(szBuffer, szString, nBufSize);
szBuffer[nBufSize-1] = 0;
}
else
_tcscpy(szBuffer, szString);
return TRUE;
}



--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Thanks, that was very helpful.

-Ab.

Jochen Kalmbach said:
Hi Abubakar!
its a c++ question. If I have the following code:

TCHAR * getsomestring()
{
TCHAR buff[2000];
// here I copy something in the buffer using wcscpy.

return buff;
}

Is this a valid code? I mean I see it as a buff allocated on the stack which
should be destroyed on the return of the function but I return it and some
other code will still have a pointer to it and try to read it. It works fine
but I wanted to clear the concept.

It is a valid code (by the means: it will be compiled without error),
but it will couse some real trouble...

In general: It is not recomended to returns strings!
If you use C++, please use come string-class, then it is safe:

std::string getsomestring()
{
return "hello world";
}


If you have no string-class, then please use the common approch to let
the caller provide the buffer for the string:


BOOL getsomestring(LPTSTR szBuffer, SIZE_T nBufSize)
{
if ( (szBuffer == NULL) || (nBufSize <= 0) )
return FALSE;
TCHAR szString[] = TEXT("Hello wolrd");
if (_tcslen(szString) > nBufSize)
{
_tcsncpy(szBuffer, szString, nBufSize);
szBuffer[nBufSize-1] = 0;
}
else
_tcscpy(szBuffer, szString);
return TRUE;
}



--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
I'm thinking if you just change the line:

TCHAR buff[2000];

to:

TCHAR buff* = new TCHAR[2000] ;

then all will be good because you are allocating memory for 'buff', not
storing it on the stack, and hence 'buff' can be passed without fear of
being 'lost' on the stack...

[==P==]
 
Back
Top