473,385 Members | 1,898 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.

Question regarding functions returning reference

Hi,

I am aware that when a function returns a reference, one should never
return a local variable to that function, since that variable will be
removed from the stack and the reference will be dangling.

Therefore, this is bad:
double& GetWeeklyHours()
{
double h =
46.50;
return h;
}

However, why this example (found in http://www.functionx.com/cpp/example...nreference.htm)
seems valid:
double& GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}

Where is the reference &hours created ? Shouldn't it be destroyed when
the function exists too ? Is that ok ? Is that even elegant ?

Thanks for your opinions.
Sep 3 '08 #1
9 1231
zebulon wrote:
Hi,

I am aware that when a function returns a reference, one should never
return a local variable to that function, since that variable will be
removed from the stack and the reference will be dangling.

Therefore, this is bad:
double& GetWeeklyHours()
{
double h =
46.50;
return h;
}

However, why this example (found in http://www.functionx.com/cpp/example...nreference.htm)
seems valid:
double& GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
Yes, I test it Using two different compilers, all compile with NO errors
or warnings.

But it seems the function return a reference to a local variable.
I have the same question with you.
Someone can explain it. Thanks.
>
Where is the reference &hours created ? Shouldn't it be destroyed when
the function exists too ? Is that ok ? Is that even elegant ?

Thanks for your opinions.
Sep 3 '08 #2
On Sep 3, 3:04*pm, asm23 <asmwarr...@gmail.comwrote:
Yes, I test it Using two different compilers, all compile with NO errors
or warnings.
gcc using -Wall returns a warning for the first example, but not the
second one. This is on a Unix machine.
Sep 3 '08 #3
zebulon wrote:
On Sep 3, 3:04 pm, asm23 <asmwarr...@gmail.comwrote:
>Yes, I test it Using two different compilers, all compile with NO errors
or warnings.

gcc using -Wall returns a warning for the first example, but not the
second one. This is on a Unix machine.
I compiled in Visual C++ 6.0 and Intel C++ 9.1, with the default option,
there is no errors or warnings.
Sep 3 '08 #4
zebulon wrote:
However, why this example (found in
http://www.functionx.com/cpp/example...nreference.htm) seems
valid: double& GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}

Where is the reference &hours created ? Shouldn't it be destroyed when
the function exists too ? Is that ok ? Is that even elegant ?
Please delete your bookmark to this site immediately: This is complete
bullshit written by an idiot. You are still returning a reference to a
value that gets destroyed on function exit.

Sep 3 '08 #5
On Sep 3, 3:23*pm, Marco Manfredini <ok_nospam...@phoyd.netwrote:
Please delete your bookmark to this site immediately: This is complete
bullshit written by an idiot. You are still returning a reference to a
value that gets destroyed on function exit.
Hi Marco,

Thanks for the answer and this actually confirms what I was
suspecting. Unfortunately, this page is ranking second in Google when
querying for "c++ function returning reference"... Aside, g++ does not
warn about the issue in the second example, but the first one only
(using -Wall), maybe a limitation in the warning detection system ?

Zebulon
Sep 3 '08 #6
On Sep 3, 7:33*am, zebulon <zebulonor...@googlemail.comwrote:
On Sep 3, 3:23*pm, Marco Manfredini <ok_nospam...@phoyd.netwrote:
Please delete your bookmark to this site immediately: This is complete
bullshit written by an idiot. You are still returning a reference to a
value that gets destroyed on function exit.

Hi Marco,

Thanks for the answer and this actually confirms what I was
suspecting. Unfortunately, this page is ranking second in Google when
querying for "c++ function returning reference"... Aside, g++ does not
warn about the issue in the second example, but the first one only
(using -Wall), maybe a limitation in the warning detection system ?
Because it's syntactically correct. The compiler is not required to
issue a diagnostic.
What you have is UB (undefined behavior).
Sep 3 '08 #7
On Sep 3, 4:11*pm, red floyd <redfl...@gmail.comwrote:
Because it's syntactically correct. *The compiler is not required to
issue a diagnostic.
What you have is UB (undefined behavior).
Yes, but that would be the case for the first case too. And there g++
catches the problem. Actually -Wall does not only catch syntax errors,
but also programming errors (as they are warnings, not errors).

Zebulon
Sep 3 '08 #8
zebulon wrote:
Thanks for the answer and this actually confirms what I was
suspecting. Unfortunately, this page is ranking second in Google when
querying for "c++ function returning reference"... Aside, g++ does not
warn about the issue in the second example, but the first one only
(using -Wall), maybe a limitation in the warning detection system ?
Yes, it is a limitation. Compilers cannot detect all occurrences of
undefined behaviour, nor are they required to by the C++ standard.

You can't count on getting a warning even in blatant cases such as this:

int main()
{
int array[3];
array[5] = 0;
}
--
Christian Hackl
Sep 3 '08 #9
On Sep 3, 4:51*pm, Christian Hackl <ha...@sbox.tugraz.atwrote:
Yes, it is a limitation. Compilers cannot detect all occurrences of
undefined behaviour, nor are they required to by the C++ standard.

You can't count on getting a warning even in blatant cases such as this:
Indeed. Thanks for the info.

Zebulon
Sep 3 '08 #10

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

Similar topics

11
by: kazack | last post by:
I am under the the impression that a variable is what is stored in a memory address and a pointer is the memory address of the variable? I was looking to use a function that does not return a...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
2
by: Terry Lee Tucker | last post by:
Let me explain: I have written a "C" function which contains calls to other functions, all of which work with an API to a mileage database product called PCMiler. These functions make...
3
by: xllx.relient.xllx | last post by:
I have a question about an example presented in the book "INFORMIT C++ Reference Guide" by Danny Kalev: <code> string getmagicword() ( return string("Supercalifragilisticexpialidocious"); );
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
35
by: rebeccatre | last post by:
hi can Variant archiving setTimout('.. capability be done without using it? :-)
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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:
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
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: 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.