Hi,
We cannot return a reference to an automatic variable from a function, as per
the ANSI C++ standard the behaviour is undefined. Does this hold for inline
functions too? or can I return a reference to the automatic variable from the
inline function. I meant automatic variable by stack variable strictly local to
that function.
Thanks,
Sreenivas. 8 3729
"Sreenivas" <sr******@gmail .com> wrote in message
news:e6******** *************** ***@posting.goo gle.com... Hi, We cannot return a reference to an automatic variable from a function, as per the ANSI C++ standard the behaviour is undefined. Does this hold for inline functions too?
Yes
john
Sreenivas posted: Hi, We cannot return a reference to an automatic variable from a function, as per the ANSI C++ standard the behaviour is undefined. Does this hold for inline functions too? or can I return a reference to the automatic variable from the inline function. I meant automatic variable by stack variable strictly local to that function. Thanks, Sreenivas.
The following two functions are equivalent in that they give the caller
access to an object which has already been destroyed:
int& Monkey()
{
int k = 4;
return k;
}
int& Ape()
{
int k = 4;
return &k;
}
As regards inline functions, they work exactly as do everyday functions.
Their static variables work exactly the same too. There's only two
differences between an inline function and a normal function:
1) It has "inline" written before it.
2) You're not violating the One Definition Rule if you define it more than
once, eg. the following is *il*legal:
int Blah() { return 5; }
int Blah() { return 5; }
While the following is perfectly legal:
inline int Blah() { return 5; }
inline int Blah() { return 5; }
(Or maybe I'm mistaken there? Perhaps you can define inline functions as
many times as you want... except it must be once per translation unit?)
Anyway, the result is that you can stick an inline function in a header
file!
-JKop
"JKop" <NU**@NULL.NULL > schrieb im Newsbeitrag
news:LT******** ***********@new s.indigo.ie... Sreenivas posted:
int Blah() { return 5; } int Blah() { return 5; }
While the following is perfectly legal:
inline int Blah() { return 5; } inline int Blah() { return 5; }
(Or maybe I'm mistaken there? Perhaps you can define inline functions as many times as you want... except it must be once per translation unit?)
IMHO "inline" solves only the linker error of linking multiple functions
with the same signature, as the compiler and the linker will typically keep
track of them over all compilation units, whereas including the same
function in the same compilation unit twice leads to a compiler error.
Regards
Michael
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<2t******* ******@uni-berlin.de>... "Sreenivas" <sr******@gmail .com> wrote in message news:e6******** *************** ***@posting.goo gle.com... Hi, We cannot return a reference to an automatic variable from a function, as per the ANSI C++ standard the behaviour is undefined. Does this hold for inline functions too?
Yes
john
const int& junk()
{
const int junk = 11;
return junk;
}
int main(...)
{
int basket; // const int & basket = junk(); __This cud give unexpected
// behaviour__
basket = junk(); // but what about this????????
return 0;
}
does this code snippet give the expected bahaviour all the time?
can i rely on this code??
-Sreenivas
> const int& junk() { const int junk = 11; return junk; }
int main(...) { int basket; // const int & basket = junk(); __This cud give unexpected // behaviour__
basket = junk(); // but what about this???????? return 0; }
does this code snippet give the expected bahaviour all the time? can i rely on this code?? -Sreenivas
I don't know, I would ask this question again where someone who does know
can answer it.
john
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<2t******* ******@uni-berlin.de>... const int& junk() { const int junk = 11; return junk; }
int main(...) { int basket; // const int & basket = junk(); __This cud give unexpected // behaviour__
basket = junk(); // but what about this???????? return 0; }
does this code snippet give the expected bahaviour all the time? can i rely on this code?? -Sreenivas
Would someone acknowledge this? whether this explanation is correct or
not.
when junk() returns a reference to an object, infact it's value wud b
copied into _basket_, as _basket_ is an object itself and not a
reference and it has it's own memory allocated. so, if this statement
is executed as an atomic statement(in multi-threaded env) it shouldn't
give any problems as per my understanding.( what happens if it's single
threaded env? i see no problems)
PS: to understand the above explanation refer the code above
"Sreenivas" <sr******@gmail .com> wrote in message
news:e6******** *************** ***@posting.goo gle.com... "John Harrison" <jo************ *@hotmail.com> wrote in message
news:<2t******* ******@uni-berlin.de>... const int& junk() { const int junk = 11; return junk; }
int main(...) { int basket; // const int & basket = junk(); __This cud give
unexpected // behaviour__
basket = junk(); // but what about this???????? return 0; }
does this code snippet give the expected bahaviour all the time? can i rely on this code?? -Sreenivas
Would someone acknowledge this? whether this explanation is correct or not.
when junk() returns a reference to an object, infact it's value wud b copied into _basket_, as _basket_ is an object itself and not a reference and it has it's own memory allocated. so, if this statement is executed as an atomic statement(in multi-threaded env) it shouldn't give any problems as per my understanding.( what happens if it's single threaded env? i see no problems)
PS: to understand the above explanation refer the code above
I think the code is wrong. The reference refers to an object which has been
destroyed. Multithreading or single threading has nothing to do with it.
john const int& junk() { const int junk = 11; return junk; }
Returning a reference to a local object, giving the calling function access
to an object which no longer exists. Let me know how that goes!
Maybe use of the "auto" keyword would make it more obvious to you:
int const& junk()
{
int const auto junk = 1;
return junk;
}
int main(...)
No comment.
{ int basket; // const int & basket = junk(); __This cud give unexpected // behaviour__
basket = junk(); // but what about this????????
Here you're doing an assignment from an object that no longer exists. Let me
know how it goes!
return 0; }
does this code snippet give the expected bahaviour all the time? can i rely on this code??
Undefined Behaviour.
-JKop This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Richard Hayden |
last post by:
Hi,
I have the following code:
/******************************** file1.c
#include <iostream>
extern void dummy();
inline int testfunc() {
|
by: Grumble |
last post by:
Hello everyone,
As far as I understand, the 'inline' keyword is a hint for the compiler
to consider the function in question as a candidate for inlining, yes?
What happens when a function with extern linkage is inlined? Should the
compiler still export the function?
Or is an inlined function implicitly static?
|
by: sam |
last post by:
Hi,
I can't figure out what is the problem of the following coding.
#ifndef __PARSER__
#define __PARSER__
#include <iostream>
#include <string>
#include <fstream>
//#include <exception>
|
by: RainBow |
last post by:
Greetings!!
I introduced the so-called "thin-template" pattern for controlling the
code bloat caused due to template usage.
However, one of the functions in the template happens to be virtual as
well. To support thin-template, I need to make virtual function as
inline.
Now, I know that compiler would generate an out-of-line copy when it
|
by: Ondrej Spanel |
last post by:
I though that inline functions should be always visible only in the
compilation unit where they are defined - meaning if compiler cannot inline
them, they should be handled as if declared static. However sample attached
shows VC compiler does not work this way (tested in .NET 2003). When you
compile the sample with inlining enabled (like in default Release config),
the output is A1 = 1, A2 = 2. When run with inlining disabled (Debug),...
| |
by: jamihuq |
last post by:
Hello,
I would like to convert the following inline function to a macro. Can
someone help?
Thx
Jami
inline
char *
fromDESC(const char * &aDesC)
|
by: toton |
last post by:
Hi,
As inline is not mandetory, it depends on compiler to inline certain
function (or using switch like fior GCC), my question is there any
scope for inlining when it is not declared as inline function?
i.e compiler may choose not to inline certain inline function, but is
it free to choose a non inline function to inline it?
I have some simple one line get function, and index operators which I
want to get inlined. But due to some problem...
|
by: Martin Wells |
last post by:
Is there anything like __inline_is_supported to indicate whether a
certain C compiler supports inline?
I'm writing my code as fully portable C89, but I want to use inline.
Martin
|
by: ciccio |
last post by:
Hi,
I have a problem with my code that the compiler does not find any inline
functions which are static.
The simple code example is written below, this is what the compiler
throws at me.
] $ g++ main.cpp foo.cpp
/home/klaas/tmp/cciAcYgl.o: In function `main':
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: 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.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |