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

what does this mean?

int& mmin(const int &a, const int &b)
{
int c=a+b;
return c;
}

---------------

I got confused... variable c is initialized in the function "mmin". It
should be on the stack. After the function is done, the storage of "c"
should be gone too...

That does the "int &" do as a reference to a non-existing variable?

Suprisingly, it worked with no problem...

Anybody can explain?


Sep 24 '06 #1
12 2119
Michael posted:
int& mmin(const int &a, const int &b)

This is a function called "mmin". It returns a reference to a non-const
int. It takes as arguments two references to const int's.

{
int c=a+b;
return c;

Here you return a reference to a local variable -- the local variable will
have been destroyed by the time the calling function gets to use it.

}

---------------

I got confused... variable c is initialized in the function "mmin". It
should be on the stack. After the function is done, the storage of "c"
should be gone too...

Yes you're right, "c" will have been destroyed.

What does the "int &" do as a reference to a non-existing variable?

Suprisingly, it worked with no problem...

Anybody can explain?

The following works on my own system too:

#include <iostream>

int &Func()
{
int i = 7;
return i;
}

int main()
{
std::cout << Func() << '\n';
}

The current International C++ Standard, however, does not define the
behaviour of this program.

The program attempts to access an object which has been destroyed.

--

Frederick Gotham
Sep 24 '06 #2
Frederick Gotham wrote:
>
The following works on my own system too:
It only works most of the time. It will fail if an interrupt comes along
between the time that Func returns and the result is passed to the
stream inserter. That's a very narrow window, which is why you don't see
it happen too often. But if you run the program enough times, it will
eventually fail.

The point being to underscore that not showing visible symptoms is not
the same as working correctly.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 24 '06 #3
Frederick Gotham wrote :
Here you return a reference to a local variable -- the local variable will
have been destroyed by the time the calling function gets to use it.
Actually, no, it's UB.
>
Sep 24 '06 #4
Michael wrote:
int& mmin(const int &a, const int &b)
{
int c=a+b;
return c;
}

---------------

I got confused... variable c is initialized in the function "mmin". It
should be on the stack.
Standard C++ doesn't require a stack. It defines that variable to be an
automatic one.
After the function is done, the storage of "c" should be gone too...
Well, it might or might not. You can't rely on anything here. The behavior
is undefined.
That does the "int &" do as a reference to a non-existing variable?
Yes.
Suprisingly, it worked with no problem...

Anybody can explain?
Undefined behavior means that anything can happen.

Sep 24 '06 #5
loufoque posted:
>Here you return a reference to a local variable -- the local variable
will have been destroyed by the time the calling function gets to use
it.

Actually, no, it's UB.

You sure about that? As far as I know, the behaviour of the following
program is well-defined:

int &Func()
{
int i; return i;
}

int main()
{
Func();
}

Similarly, I believe the following programming is also well-defined:

int Func() { /* No return statement */ }

int main() { Func(); }

--

Frederick Gotham
Sep 24 '06 #6
Frederick Gotham wrote:
loufoque posted:
>>Here you return a reference to a local variable -- the local variable
will have been destroyed by the time the calling function gets to use
it.

Actually, no, it's UB.


You sure about that? As far as I know, the behaviour of the following
program is well-defined:

int &Func()
{
int i; return i;
}

int main()
{
Func();
}
I don't know about that one. But it looks fishy.

Similarly, I believe the following programming is also well-defined:

int Func() { /* No return statement */ }

int main() { Func(); }
[6.6.3/2]
.... Flowing off the end of a function is equivalent to a return with no
value; this results in undefined behavior in a value-returning function.
Best

Kai-Uwe Bux
Sep 24 '06 #7
Kai-Uwe Bux posted:
[6.6.3/2]
... Flowing off the end of a function is equivalent to a return with no
value; this results in undefined behavior in a value-returning function.

Out of interest, I think it's legal in C89 so long as you don't try to access
the return value.

--

Frederick Gotham
Sep 24 '06 #8
In article <lH*******************@news.indigo.ie>, fg*******@SPAM.com
says...
Kai-Uwe Bux posted:
[6.6.3/2]
... Flowing off the end of a function is equivalent to a return with no
value; this results in undefined behavior in a value-returning function.


Out of interest, I think it's legal in C89 so long as you don't try to access
the return value.
That's correct (section 6.6.6.4): "If a return statement without an
expression is executed, and the value of the function call is used by
the caller, the behavior is undefined. Reaching the } that terminates a
function is equivalent to executing a return statement without an
expression."

In C99, however, the rule changes to be like in C++ (section 6.8.6.4/1,
a constraint on the return statement): "A return statement without an
expression shall appear only in a function whose return type is void."

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 25 '06 #9
Jerry Coffin posted:
In C99, however, the rule changes to be like in C++ (section 6.8.6.4/1,
a constraint on the return statement): "A return statement without an
expression shall appear only in a function whose return type is void."

I read that to mean that you can't have:

int Func() { return; }

It doesn't suggest to me that the following is broken:

int Func() {}

--

Frederick Gotham
Sep 27 '06 #10
Frederick Gotham wrote:
Jerry Coffin posted:
>In C99, however, the rule changes to be like in C++ (section
6.8.6.4/1, a constraint on the return statement): "A return
statement without an expression shall appear only in a function
whose return type is void."


I read that to mean that you can't have:

int Func() { return; }

It doesn't suggest to me that the following is broken:

int Func() {}
Clarification: "broken" most likely means "ill-formed" here.
Sep 27 '06 #11
Victor Bazarov posted:
>It doesn't suggest to me that the following is broken:

int Func() {}

Clarification: "broken" most likely means "ill-formed" here.

I use the term, "broken", when I've to pause for more than two seconds to
consider whether it's "undefined behaviour" or "ill-formed"... it's a blanket
term!

--

Frederick Gotham
Sep 27 '06 #12
Frederick Gotham wrote:
Victor Bazarov posted:
>>It doesn't suggest to me that the following is broken:

int Func() {}

Clarification: "broken" most likely means "ill-formed" here.


I use the term, "broken", when I've to pause for more than two
seconds to consider whether it's "undefined behaviour" or
"ill-formed"... it's a blanket term!
Trying to pull some wool over our eyes, eh?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '06 #13

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
86
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that:...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
2
by: Steve Richter | last post by:
What does the "." mean in the following sql script stmts? use GO if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table ....
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
1
by: Frank Rizzo | last post by:
Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: JoeC | last post by:
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; What does this mean? I have seen v=&var->member.thing; but what does it mean when you...
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...
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
Oralloy
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,...
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

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.