Question regarding functions returning reference

Z

zebulon

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/examples/returnreference.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.
 
A

asm23

zebulon said:
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/examples/returnreference.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.
 
Z

zebulon

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.
 
A

asm23

zebulon said:
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.
 
M

Marco Manfredini

zebulon said:
However, why this example (found in
http://www.functionx.com/cpp/examples/returnreference.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.
 
Z

zebulon

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
 
R

red floyd

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).
 
Z

zebulon

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
 
Z

zebulon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top