Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > dangling reference

Reply
Thread Tools

dangling reference

 
 
Hans Van den Eynden
Guest
Posts: n/a
 
      10-14-2004
I thought that integers resized on the stack and that a value type was
just defined in the scope of the function. And when the function
returns the local variables were removed from the stack. But what is
the explication of this code??

int& dangling_reference();

int main(int argc, char *argv[])
{
int a;
a= dangling_reference();
cout<<a<<endl;
cin.get();
}



int& dangling_reference() {
int x=3;
return x;
}
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-14-2004

"Hans Van den Eynden" <> wrote in message
news: om...
>I thought that integers resized on the stack and that a value type was
> just defined in the scope of the function. And when the function
> returns the local variables were removed from the stack. But what is
> the explication of this code??
>
> int& dangling_reference();
>
> int main(int argc, char *argv[])
> {
> int a;
> a= dangling_reference();
> cout<<a<<endl;
> cin.get();
> }
>
>
>
> int& dangling_reference() {
> int x=3;
> return x;
> }


That code has undefined behaviour, because you are using a reference to an
object that has been destroyed. What were you expecting? What do you see?

john


 
Reply With Quote
 
 
 
 
Method Man
Guest
Posts: n/a
 
      10-14-2004

"Hans Van den Eynden" <> wrote in message
news: om...
> I thought that integers resized on the stack and that a value type was
> just defined in the scope of the function. And when the function
> returns the local variables were removed from the stack. But what is
> the explication of this code??
>
> int& dangling_reference();
>
> int main(int argc, char *argv[])
> {
> int a;
> a= dangling_reference();
> cout<<a<<endl;
> cin.get();
> }
>
>
>
> int& dangling_reference() {
> int x=3;
> return x;
> }


This is UB. If you are getting the correct result (printing "3"), then
you're just lucky. This probably occured because your program is small and
the contents of the memory address of 'x' are still intact. Regardless, you
should not *expect* your code to work.


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-15-2004
Method Man wrote:
>

[snip]
>
> This is UB. If you are getting the correct result (printing "3"), then
> you're just lucky.




Some would say: He was unlucky.
The reason: If he were lucky, the program would have crashed immediatly
indicating that there is a problem somewhere. So he is unlucky in that there
is a problem which was not found during testing and waits as a time bomb for
the moment the end user first tries that program (and according to Murphy
crashes immediatly)

--
Karl Heinz Buchegger

 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      10-15-2004
Hans Van den Eynden posted:

> I thought that integers resized on the stack and that a value type was
> just defined in the scope of the function. And when the function
> returns the local variables were removed from the stack. But what is
> the explication of this code??
>
> int& dangling_reference();
>
> int main(int argc, char *argv[])
> {
> int a;
> a= dangling_reference();
> cout<<a<<endl;
> cin.get();
> }
>
>
>
> int& dangling_reference() {
> int x=3;
> return x;
> }



The following would be valid:

int& Blah()
{
int &x = *new int(3);

return x;
}


But then ofcourse the calling function would have to call "delete".

In circumstances like this, think to yourself, "Could I use a pointer
here?". If the answer's no, then you can't use a reference either. For
instance, the following may be more obviously invalid to you:

int* Blah()
{
int x = 7;

return &x;
}


A lot of compiler's warn you when you do the above.


Warning: line 5: Returning reference to local object


-JKop

 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      10-15-2004

> A lot of compiler's warn you when you do the above.



No, I'm not illiterate, that was a typo! "compilers".


-JKop
 
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
Dangling Reference. Idiom Belebele C++ 10 02-08-2007 10:43 PM
dangling reference Nick Keighley C++ 1 04-03-2005 11:42 AM
DesignRules:331 Dangling RAMB16A output: (Help) rootz anabo VHDL 0 02-03-2005 04:04 PM
dangling reference Hans Van den Eynden Java 1 10-16-2004 09:20 AM
Dangling Reference kaede C++ 4 10-28-2003 09:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57