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. 9 1252
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.
On Sep 3, 3:04*pm, asm23 <asmwarr...@gma il.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.
zebulon wrote:
On Sep 3, 3:04 pm, asm23 <asmwarr...@gma il.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.
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.
On Sep 3, 3:23*pm, Marco Manfredini <ok_nospam...@p hoyd.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
On Sep 3, 7:33*am, zebulon <zebulonor...@g ooglemail.comwr ote:
On Sep 3, 3:23*pm, Marco Manfredini <ok_nospam...@p hoyd.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).
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
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
On Sep 3, 4:51*pm, Christian Hackl <ha...@sbox.tug raz.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 value void
whatever(whatever) and not have a variable global but in main and change the
value in the void whatever function.
using namespace std;
void whatever(int);
|
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 function to
verify the name fields, age, email and gender.
My question is: if I create a function for each field like the code
below, what would be the best way to organize the functions and call
them? Would I need one main function and place...
|
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 get no error...
2) Length & Length :: operator+ ( void ) const {return * this;}
... I get the error -> 'return' : cannot convert from 'const class Length' to
|
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 connections to the PCMiler databae, do error checking on the
origin and destination, get directions, etc. There is only one function
loaded into postgres and it makes calls to everything else. I create a shared
object library containing one object file:...
|
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");
);
| |
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 image rotates. I
cannot figure out how to accomplish the 3 second delay. My code is
pasted below:
function randPic(){
randPic1();
randPic2();
|
by: rebeccatre |
last post by:
hi can Variant archiving setTimout('.. capability be done without
using it? :-)
|
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. The global
variables and global functions are hidden to prevent from accessing by the
programmers. All global functions share global variables.
Only very few global functions are allowed to be reusability for the
programmers to use. Few...
|
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 allocator write forward declaration then allocation for void*
rather than directly wrote allocator first ?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |