473,382 Members | 1,705 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,382 software developers and data experts.

Is it safe to return "char*" from functions?

Hello,

I've seen in some programs that they provide functions which has the
following prototype:
char *func1();

However I'm not sure it's safe. If the char array beeing returned is a local
variable such as:

char *func1()
{
char string[] = "Hello World";
return string;
}

then you got a problem because the string will be stored on stack which will
be de-allocated upon function return. So in reality the string is located on
a memory position which is availble for anyones use.
If keywords such as static or global data is returned the situation might be
different.
If you use new/malloc on the data you want to return it's safe but then
again you're making a memory leak unless the caller knows it's nessecary to
clean up.

Could anyone clear this up for me.

Thanks in advance.

-- John
Jul 19 '05 #1
2 10451
"John Eskie"
... then you got a problem because the string will be stored on stack which will
be de-allocated upon function return. So in reality the string is located on
a memory position which is availble for anyones use.
yes
If keywords such as static or global data is returned the situation might be
different.
uhu
If you use new/malloc on the data you want to return it's safe but then
again you're making a memory leak unless the caller knows it's nessecary to
clean up.


indeed

Jul 19 '05 #2
"John Eskie" <cy******@l115.langkaer.dk> wrote in message
news:3f***********************@dread12.news.tele.d k...
Hello,

I've seen in some programs that they provide functions which has the
following prototype:
char *func1();

However I'm not sure it's safe. If the char array beeing returned is a local variable such as:

char *func1()
{
char string[] = "Hello World";
return string;
}

then you got a problem because the string will be stored on stack which will be de-allocated upon function return. So in reality the string is located on a memory position which is availble for anyones use.
If keywords such as static or global data is returned the situation might be different.
If you use new/malloc on the data you want to return it's safe but then
again you're making a memory leak unless the caller knows it's nessecary to clean up.

Could anyone clear this up for me.


It seems you understand the issues with returning pointers very well. The
C++ solution for this particular example is to return std::string instead of
a char pointer, and your resource management problems are solved.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Jul 19 '05 #3

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

Similar topics

12
by: Vaca Louca | last post by:
Hello, I write an ISAPI authentication module which uses Berkeley DB and want it to be as efficient as possible. Both ISAPI and BerkeleyDB use arrays of chars (char *) to pass and receive...
4
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain...
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
34
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
3
by: Mahesh | last post by:
#include <stdio.h> #include <string.h> #include <malloc.h> int main(void) { int key = 120; char *s = "String to Encrypt using XOR"; char *ss = s; char echar; char dchar;
14
by: Anna | last post by:
I try to put 8 int bit for example 10100010 into one character of type char(1 octet) with no hope . Could anyone propose a simple way to do it? Thank you very much.
0
by: Ben Voigt [C++ MVP] | last post by:
Chip Gore wrote: Actually, because you are using the C++/CLI compiler, microsoft.public.dotnet.languages.vc is the most appropriate place to discuss this. To be useful from C# and other .NET...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.