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

Are string literals local or otherwise temporary?

Kza
This is probably just newbie silliness to all you c++ gurus but I just
noticed an interesting compiler warning today while compiling this
function:

const std::string& someclass::somemethod()
{
return "This is a string literal";
}

The compiler warned me that an address of a local variable or a
temporary value is being returned.

Does this make sense? Arent string literals basically static chunks of
memory in the data segment?

Why would this be created on the stack?

Or whats going on here and how can I be sure the string is always
available to the caller of the method?

Its no big drama, I am sure I could fix it, but I am just trying to
understand the language etc and how it all works behind the scenes.

Thanks a lot

Kurt

Feb 14 '06 #1
2 1738
"Kza" <kz****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
: This is probably just newbie silliness to all you c++ gurus but I just
: noticed an interesting compiler warning today while compiling this
: function:
:
: const std::string& someclass::somemethod()
: {
: return "This is a string literal";
: }
:
: The compiler warned me that an address of a local variable or a
: temporary value is being returned.
:
: Does this make sense? Arent string literals basically static chunks of
: memory in the data segment?
Yes. But note that string literals are of type (const) char[].

: Why would this be created on the stack?
: Or whats going on here and how can I be sure the string is always
: available to the caller of the method?
You are not returning an array of characters (or a char const*),
but an object of type std::string (a class defined in the standard
C++ library).
Therefore, your return statement involves an implicit conversion,
and is equivalent to:
return std::string("This is a string literal");
You are indeed returning a reference to the temporary std::string
object being created for the conversion.

: Its no big drama, I am sure I could fix it, but I am just trying to
: understand the language etc and how it all works behind the scenes.

I hope this helped,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Feb 14 '06 #2
* Kza:
This is probably just newbie silliness to all you c++ gurus but I just
noticed an interesting compiler warning today while compiling this
function:

const std::string& someclass::somemethod()
{
return "This is a string literal";
}

The compiler warned me that an address of a local variable or a
temporary value is being returned.


You're returning a reference to a temporary object.

Change the result type to

std::string

Nothing more.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 14 '06 #3

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

Similar topics

12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
4
by: Locusta | last post by:
Hello, I have been struggeling for replacing a string in a string. The snippet from the program below replaces the <, & and > with the XML equivalent values. In the program, I allocate space...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
4
by: Quentin Yuan | last post by:
I always consider that the constant character strings of which literal value are the same lay out at the same logic address, in another words, every constant character string have only one copy in...
7
by: lithiumcat | last post by:
Hi, I'm not yet very confident in my use of standard terminology, so please be kind if I'm mis-calling something, I will do my best no to make it again once pointed out. I'm wondering what is...
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:
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: 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: 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...
0
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,...
0
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...

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.