473,769 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10494
"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.t ele.dk...
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.merke rk(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
2033
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 information. I know that C++ strings are supposed to be the "right" way to handle strings but I suspect that converting "char *" to string
4
10724
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 ASCII characters in the 0-127 range, although I had thought that I might want to use the 128-255 range for special symbols or foreign character codes. This all worked OK for a long time, but a recent update to the compiler on my system has...
26
11751
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
34
2688
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
20
3566
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
2352
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
307
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
1395
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 languages, the member functions of your ref class should accept parameters of type System::String^. The C++/CLI compiler provides some pretty simple methods for getting the data as native C++ characters if you need to, for example the...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.