473,625 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

return "chars"

char* foo () {
return "abc";
}

I compiled the above code both with MSVC and GCC for PPC. The string
"abc" is generated as a global entity. Thus (1) foo doesn't return a
temporary
and (2) no deallocation is necessary.

What does the standard say about this? Is this a "feature" I can rely on on
every
platform?
Jan 14 '06 #1
1 2011
vl106 said:
char* foo () {
return "abc";
}

I compiled the above code both with MSVC and GCC for PPC. The string
"abc" is generated as a global entity. Thus (1) foo doesn't return a
temporary
and (2) no deallocation is necessary.

What does the standard say about this?
"A character string literal has static storage duration"

and

"An object declared with external or internal linkage, or with the
storage-class specifier static has static storage duration. For such an
object, storage is reserved and its stored value is initialized only once,
prior to program startup. The object exists and retains its last-stored
value throughout the execution of the entire program."

Is this a "feature" I can rely on on every platform?


Yes, string literals have static storage duration, and exist for the whole
lifetime of the program.

Just one suggestion, though - I really think you ought to return const char
* rather than char *. You wouldn't want your calling code to try to
/modify/ that string, would you now?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 14 '06 #2

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

Similar topics

5
2065
by: sbd! | last post by:
I had a problem with string which have some \ chars at the end of the string. For example: $String = "I want this **** out of here\\\\\\\\\"; I needed to remove it useing regular expression...i thinked that something like "/\\+$/" is enough but i was wrong. I made some tries and finaly i removed "\" chars it with "/\\\+$/". It works (i'm proud with myself :)), but i can't understand why \\\ matchs single \, and \\ not...
1
3961
by: st | last post by:
Hi, I'm using xmlDocument.Save(xmlTextWriter) to create an Excel-readable file. All works well, except where I've replaced the carriage return chars in the .innertext to XML-compliant " "; It gets changed to "&#10" and doesn't render new lines in the Excel sheet. Can anyone help? Many thanks,
3
1158
by: Luminal | last post by:
greetings i'm developing an C# application using Access as database and I'm having problems inserting data containing the ' char and the " char. What is the best practice to insert this chars on database? TIA Lum
7
3956
by: Luminal | last post by:
Greetings I'm having some problems on my C# application. I'm using an access database and I'm not able to do select queries with the ' character. My code is this: // some previous code like open connection string sqlStatement="SELECT oid FROM formatos WHERE formato='cd\\'s'";
8
7456
by: js | last post by:
HI guys, How do you write Perl's print a ... z, A ... Z, "\n"' in Python In Python?
8
8736
by: Jack Brown | last post by:
Hi there, I need to create a unique identifier for my own internal needs and am happy to rely on "Guid.NewGuid()" to pull this off. I'd like to know if .NET offers any competing alternative however. Perhaps there's something with a richer set of functions for instance and it might offer potential advantages over a GUID. Am just exploring the possibilities for now. Thanks in advance.
3
1281
by: gizz | last post by:
Hi, Is there a difference between : var myStr = "This is some text" ; and, var myStr = 'This is some text' ;
1
2284
by: ofir | last post by:
I don't understand. according to MSDN session id sould be LONG but when I look at the value of Context.Session.SessionID in my asp.net application I get a 24 chars value
2
3853
by: Gabriela | last post by:
Hi, I would like to use VBScript regexp to locate multiple characters in a string and replace them with a single "-" char. In my case it's multiple "-" chars: E.g.: "Gabriela---Hello--How----Are-You" -->"Gabriela-Hello-How-Are- You" How is it done? Thanks, Gabi.
0
8182
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
8688
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
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
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.