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

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 1991
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
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...
1
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...
3
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...
7
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...
8
by: js | last post by:
HI guys, How do you write Perl's print a ... z, A ... Z, "\n"' in Python In Python?
8
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...
3
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
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
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.:...
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: 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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.