Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > return reference to local variable, good code?

Reply
Thread Tools

return reference to local variable, good code?

 
 
George2
Guest
Posts: n/a
 
      12-16-2007
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
 
Reply With Quote
 
 
 
 
yanlinlin
Guest
Posts: n/a
 
      12-16-2007
On 12月16日, 下午1时49分, George2 <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-16-2007
* 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: 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?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-16-2007
* yanlinlin:
> On 12鏈16鏃, 涓嬪崍1鏃49鍒, George2 <(E-Mail Removed)> wrote:
>> 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: 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?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-16-2007
* Alf P. Steinbach:
> * yanlinlin:
>> On 12鏈16鏃, 涓嬪崍1鏃49鍒, George2 <(E-Mail Removed)> wrote:
>>> 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

--
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?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-16-2007
Alf P. Steinbach wrote:
> 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 ??

--
Ian Collins.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-16-2007
* Ian Collins:
> Alf P. Steinbach wrote:
>> 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 ??


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?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-16-2007
Alf P. Steinbach wrote:
> 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"

--
Ian Collins.
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      12-16-2007
On Dec 16, 2:00 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * Ian Collins:
>
> > Alf P. Steinbach wrote:
> >> 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 ??

>
> 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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-16-2007
On Dec 16, 7:11 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * yanlinlin:


> > On 12鏈16鏃, 涓嬪崍1鏃49鍒, George2 <(E-Mail Removed)> wrote:
> >> I am reading some code from other people, there are some code like
> >> this,


> >>
Code:
> >> class Foo {
> >> };
Code:
>
> >> 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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orient茅e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S茅mard, 78210 St.-Cyr-l'脡cole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 3.17 Is it safe to return a reference to local or lexical data? PerlFAQ Server Perl Misc 0 01-28-2011 05:00 PM
return reference to local variable? zheng4t@gmail.com C++ 2 05-30-2008 05:51 AM
good code to return const reference to function local object? George2 C++ 5 01-25-2008 09:56 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
return reference to local variable ? romerun C++ 6 09-03-2004 03:03 AM



Advertisments