return reference to local variable, good code?

G

George2

Hello everyone,


I am reading some code from other people, there are some code like
this,

Code:
class Foo {

};

Foo& func()
{
	Foo foo;
	return foo;
}

int main()
{
	Foo& foo = func();

	return 0;
}

I want to confirm with you that it is not good code, since we return a
reference to local object instance, right? Even if the code has
expected function currently.


thanks in advance,
George
 
Y

yanlinlin

Hello everyone,

I am reading some code from other people, there are some code like
this,

Code:
class Foo {

};

Foo& func()
{
Foo foo;
return foo;

}

int main()
{
Foo& foo = func();

return 0;}

I want to confirm with you that it is not good code, since we return a
reference to local object instance, right? Even if the code has
expected function currently.

thanks in advance,
George

I'm afraid this code can not be compiled. To return a reference of
local non-static object is not allowed.
 
A

Alf P. Steinbach

* George2:
Hello everyone,


I am reading some code from other people, there are some code like
this,

Code:
class Foo {

};

Foo& func()
{
	Foo foo;
	return foo;
}

int main()
{
	Foo& foo = func();

	return 0;
}

I want to confirm with you that it is not good code, since we return a
reference to local object instance, right? Even if the code has
expected function currently.

It's Undefined Behavior if the function result is ever used.

Btw., could you or anyone else please confirm that you can see this posting.

It seems that all internet connections out of Norway are down at the
moment, at least via my ISP (e.g. I can't reach Microsoft's servers, nor
Google), yet articles keep dropping in in international newsgroups.

Of course, if you don't see this posting, simply don't respond.

Cheers, hth., & tia.,

- Alf
 
A

Alf P. Steinbach

* yanlinlin:
Hello everyone,

I am reading some code from other people, there are some code like
this,

Code:
class Foo {

};

Foo& func()
{
Foo foo;
return foo;

}

int main()
{
Foo& foo = func();

return 0;}

I want to confirm with you that it is not good code, since we return a
reference to local object instance, right? Even if the code has
expected function currently.

thanks in advance,
George

I'm afraid this code can not be compiled. To return a reference of
local non-static object is not allowed.

It's allowed and it should compile.

A compiler that doesn't allow it is non-conforming.

However, using that result incurs Undefined Behavior.

Cheers, & hth.,

- Alf


PS: Could you, or anyone, confirm whether you see this posting? If you
don't see it, simply do nothing.
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* yanlinlin:
Hello everyone,

I am reading some code from other people, there are some code like
this,

Code:
class Foo {

};

Foo& func()
{
Foo foo;
return foo;

}

int main()
{
Foo& foo = func();

return 0;}

I want to confirm with you that it is not good code, since we return a
reference to local object instance, right? Even if the code has
expected function currently.

thanks in advance,
George

I'm afraid this code can not be compiled. To return a reference of
local non-static object is not allowed.

It's allowed and it should compile.

A compiler that doesn't allow it is non-conforming.

However, using that result incurs Undefined Behavior.

Cheers, & hth.,

- Alf


PS: Could you, or anyone, confirm whether you see this posting? If you
don't see it, simply do nothing.

And I'm sorry this is EXTREMELY OFF-TOPIC, but perhaps someone knows how
this can be?

It seems the connection problems are protocol-dependent.

Although I can't read my mail (GMail), I can read the New York Times,
but pinging that server fails:

----------------------------------------------------------------------
C:\Documents and Settings\Alf> wget www.nytimes.com
--07:17:34-- http://www.nytimes.com/
=> `index.html'
Resolving www.nytimes.com... 199.239.136.200, 199.239.136.245,
199.239.137.200, ...
Connecting to www.nytimes.com|199.239.136.200|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]

[ <=>
] 104 331 187.81K/s

07:17:35 (187.31 KB/s) - `index.html' saved [104331]


C:\Documents and Settings\Alf> ping www.nytimes.com

Pinging www.nytimes.com [199.239.136.200] with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 199.239.136.200:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

C:\Documents and Settings\Alf>_
-------------------------------------------------------------------------

If can see this posting, please follow-up, e.g. "off-topic Alf! Get lost!"

Of course, if can't see it, simply do nothing.

Cheers, and sorry for off-topic posting,

- Alf
 
I

Ian Collins

Alf said:
And I'm sorry this is EXTREMELY OFF-TOPIC, but perhaps someone knows how
this can be?

It seems the connection problems are protocol-dependent.

Although I can't read my mail (GMail), I can read the New York Times,
but pinging that server fails:
Is your PC infected by windows ??
 
A

Alf P. Steinbach

* Ian Collins:
Is your PC infected by windows ??

Yeah, thanks for replying. Perhaps it's also OS-dependent... :) Just
side-note on that while still very much off-topic: searching for causes
I found a "DCOM startup" service running in spite of being designated as
manual start, and since DCOM is Microsoft's super-duper entry vector for
bad beasties (e.g. infamous RPC vulnerability some years ago) I tried to
stop it but it wouldn't; I killed the process and Windows told me it had
to shut down, which it did; and having disabled that service, when I
then ran Word when Windows came up again, Word told me that "this
document couldn't be registeted and therefore can't be linked to", i.e.
Word silently invokes some DCOM -- network! -- registration when you
open a document. Jeez. Shudder. Now, I'll take the punishment for OT.

Cheers,

- Alf
 
I

Ian Collins

Alf said:
Word silently invokes some DCOM -- network! -- registration when you
open a document. Jeez. Shudder. Now, I'll take the punishment for OT.
Repeat after me "I will install OpenOffice" "I will install OpenOffice"
 
S

Salt_Peter

* Ian Collins:



Yeah, thanks for replying. Perhaps it's also OS-dependent... :) Just
side-note on that while still very much off-topic: searching for causes
I found a "DCOM startup" service running in spite of being designated as
manual start, and since DCOM is Microsoft's super-duper entry vector for
bad beasties (e.g. infamous RPC vulnerability some years ago) I tried to
stop it but it wouldn't; I killed the process and Windows told me it had
to shut down, which it did; and having disabled that service, when I
then ran Word when Windows came up again, Word told me that "this
document couldn't be registeted and therefore can't be linked to", i.e.
Word silently invokes some DCOM -- network! -- registration when you
open a document. Jeez. Shudder. Now, I'll take the punishment for OT.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

disable DCOM via registry:
http://support.microsoft.com/kb/825750

i'll second the OpenOffice option.
 
J

James Kanze

* yanlinlin:
I am reading some code from other people, there are some code like
this,
Code:
class Foo {
}; 
Foo& func()
{
Foo foo;
return foo;
} 
int main()
{
Foo& foo = func();
return 0;}
I want to confirm with you that it is not good code, since
we return a reference to local object instance, right? Even
if the code has expected function currently.
I'm afraid this code can not be compiled. To return a
reference of local non-static object is not allowed.
It's allowed and it should compile.
A compiler that doesn't allow it is non-conforming.
However, using that result incurs Undefined Behavior.

I think that if the compiler can prouve that the result is used
(and it is here), then it is allowed to reject the code. (The
case here is an interesting one, in fact---I'm assuming that the
same rules hold for references as for pointers, and that even
copying an invalid reference is undefined behavior. I've not
looked it up, however, so caveat empor.)
PS: Could you, or anyone, confirm whether you see this
posting? If you don't see it, simply do nothing.

I've seen it. (Rather obviously.) I even figured out something
on-topic to add so that I wouldn't be off topic in
responding:).
 
A

Alf P. Steinbach

* James Kanze:
* yanlinlin:
I am reading some code from other people, there are some code like
this,
Code:
class Foo {
};
Foo& func()
{
Foo foo;
return foo;
}
int main()
{
Foo& foo = func();
return 0;}
I want to confirm with you that it is not good code, since
we return a reference to local object instance, right? Even
if the code has expected function currently.
I'm afraid this code can not be compiled. To return a
reference of local non-static object is not allowed.
It's allowed and it should compile.
A compiler that doesn't allow it is non-conforming.
However, using that result incurs Undefined Behavior.

I think that if the compiler can prouve that the result is used
(and it is here), then it is allowed to reject the code. (The
case here is an interesting one, in fact---I'm assuming that the
same rules hold for references as for pointers, and that even
copying an invalid reference is undefined behavior. I've not
looked it up, however, so caveat empor.)
PS: Could you, or anyone, confirm whether you see this
posting? If you don't see it, simply do nothing.

I've seen it. (Rather obviously.) I even figured out something
on-topic to add so that I wouldn't be off topic in
responding:).

Thanks. I didn't think of reference initialization as "use". May have
to adjust my internal C++ rule-set -- at least if someone cites the
standard about this and it says so.

Cheers,

- Alf
 
Y

yanlinlin

It's allowed and it should compile.
A compiler that doesn't allow it is non-conforming.

However, using that result incurs Undefined Behavior.

Cheers, & hth.,

- Alf

Yes, I misunderstood that.
It can compile successfully, but with a warning. And the option "treat
warning as error" misguided me about it. :p
 
J

James Kanze

I didn't think of reference initialization as "use". May have
to adjust my internal C++ rule-set -- at least if someone cites the
standard about this and it says so.

To be frank, I'm not sure that the standard says this
explicitly. What I am fairly sure about is that there was an
*intent* that references could be implemented as pointers, and
that if you remap the reference operations in question as
pointers, then you do have undefined behavior. So I think that
the intent is that this be undefined behavior. (But I'm far
from sure that the standard actually expresses this intent.
Maybe a question in comp.std.c++ would be in order.)
 
P

Pavel Shved

* yanlinlin:
I am reading some code from other people, there are some code like
this,
Code:
class Foo {
}; 
Foo& func()
{
Foo foo;
return foo;
} 
int main()
{
Foo& foo = func();
return 0;}
I want to confirm with you that it is not good code, since
we return a reference to local object instance, right? Even
if the code has expected function currently.
I'm afraid this code can not be compiled. To return a
reference of local non-static object is not allowed.
It's allowed and it should compile.
A compiler that doesn't allow it is non-conforming.
However, using that result incurs Undefined Behavior.

I think that if the compiler can prouve that the result is used
(and it is here), then it is allowed to reject the code. (The
case here is an interesting one, in fact---I'm assuming that the
same rules hold for references as for pointers, and that even
copying an invalid reference is undefined behavior. I've not
looked it up, however, so caveat empor.)


How can compiler prove that the result is used? How can he prove i'm
not writing a thread that's supposed to be externally terminated in
the very same case as when the function happens to return a reference-
to-local, the reference later explicitely being used but unreachable
in practice? :)
 
N

Nick Keighley

* George2:




Hello everyone,
I am reading some code from other people, there are some code like
this,
Code:
class Foo { [QUOTE]
};[/QUOTE]

Foo& func()
{
Foo foo;
return foo;
}[/QUOTE]
[QUOTE]
int main()
{
Foo& foo = func();[/QUOTE]
[QUOTE]
return 0;
}
I want to confirm with you that it is not good code, since we return a
reference to local object instance, right? Even if the code has
expected function currently.

It's Undefined Behavior if the function result is ever used.

Btw., could you or anyone else please confirm that you can see this posting.

It seems that all internet connections out of Norway are down at the
moment, at least via my ISP (e.g. I can't reach Microsoft's servers, nor
Google), yet articles keep dropping in in international newsgroups.

Of course, if you don't see this posting, simply don't respond.

i can c u. uk
 
J

James Kanze

* yanlinlin:
I am reading some code from other people, there are some code like
this,
Code:
class Foo {
}; 
Foo& func()
{
Foo foo;
return foo;
} 
int main()
{
Foo& foo = func();
return 0;}
I want to confirm with you that it is not good code, since
we return a reference to local object instance, right? Even
if the code has expected function currently.
I'm afraid this code can not be compiled. To return a
reference of local non-static object is not allowed.
It's allowed and it should compile.
A compiler that doesn't allow it is non-conforming.
However, using that result incurs Undefined Behavior.
I think that if the compiler can prouve that the result is
used (and it is here), then it is allowed to reject the
code. (The case here is an interesting one, in fact---I'm
assuming that the same rules hold for references as for
pointers, and that even copying an invalid reference is
undefined behavior. I've not looked it up, however, so
caveat empor.)
How can compiler prove that the result is used?

By flow analysis.
How can he prove i'm not writing a thread that's supposed to
be externally terminated in the very same case as when the
function happens to return a reference- to-local, the
reference later explicitely being used but unreachable in
practice? :)

That's undefined behavior as well, as far as the standard goes,
so the compiler doesn't have to take it into account. (In fact,
of course, it's also impossible to do on most modern systems;
another process can't really look at the individual instructions
you're executing.)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top