473,378 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Returning a Pointer

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.
Nov 17 '05 #1
5 817
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.

Nov 17 '05 #2
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/
Nov 17 '05 #3
Thanks, that was very helpful.

-Ab.

"Jochen Kalmbach [MVP]" <no********************@holzma.de> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
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/

Nov 17 '05 #4
Thanks,

Ab.

"sashan" <sa*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.

Nov 17 '05 #5
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==]

"Abubakar" <ab*******@gmail.com> wrote in message
news:eT**************@tk2msftngp13.phx.gbl...
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.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
9
by: josh | last post by:
Hi, I'm converting (for learning purpose) )a program from Java to C++ and I've a doubt: In Java every argument (variable and reference types) is passed and returned in functions by-value so if I...
6
by: student1976 | last post by:
All Beginner/Intermediate level question. I understand that returning ptr to local stack vars is bad. Is returning foo_p_B from fnB() reliable all the time, so that using foo_p_A does not...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.