C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pointer to temporary

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Dimitar
Guest





PostPosted: Tue Apr 26, 2005 4:45 pm    Post subject: Pointer to temporary Reply with quote



Why the following code (anotherFunc) is wrong:

class A
{
....
};

A func()
{
A a;
...
return a;
}


void anotherFunc()
{
const A* = &func();
// use A
}

If reference is used instead of a pointer, everything is Ok.
Probably I am missing something from the standard.
The only explanation I have is that if the temporary is not referenced it
goes out of scope and gets deleted. If pointer is used, it is much more
difficult (or even impossible) for the compiler to determine when the
temporary goes out of scope. Does this sound correct?

Any comments will be appreciated.

Thanks,

Dimitar



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Wed Apr 27, 2005 8:46 am    Post subject: Re: Pointer to temporary Reply with quote



Dimitar wrote:
Quote:
Why the following code (anotherFunc) is wrong:

class A
{
...
};

A func()
{
A a;
...
return a;
}


void anotherFunc()
{
const A* = &func();
// use A
}

If reference is used instead of a pointer, everything is Ok.
Probably I am missing something from the standard.

Probably.

Quote:
The only explanation I have is that if the temporary is not referenced it
goes out of scope and gets deleted. If pointer is used, it is much more
difficult (or even impossible) for the compiler to determine when the
temporary goes out of scope. Does this sound correct?

Yes, with the only correction. Temporaries do not "go out of scope".
Their lifetimes end.

References need to be initialised and can never be "re-seated". Pointers
can be assigned to thus making them point to a different object. In order
to keep track of the lifetime of the temporary bound to a pointer, the
compiler would have to jump through hoops figuring out whether you have
changed the pointer value. IOW, the lifetime of the pointer has no
bearing on the lifetime of the temporary if it were allowed to connect
them.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Allan W
Guest





PostPosted: Wed Apr 27, 2005 8:48 am    Post subject: Re: Pointer to temporary Reply with quote



Dimitar wrote (altered for brevity)
Quote:
class A { /* ... */ };
A func() {
A a;
return a;
}
So func() returns an A by value.


Quote:
void anotherFunc()
{
const A* = &func();

This is a syntax error. I think you meant
const A*a = &func();

Quote:
// use A
}

The return value of func() is a temporary of type A.
You take the address of that temporary and save it in a.
Then the temporary goes out of scope and gets destroyed.
Your pointer is now "dangling".

Quote:
If reference is used instead of a pointer, everything is Ok.

Yes. There's an exception to the rule about the lifetime of
temporaries;
if you take a reference to it, then the lifetime extends to the
lifetime
of the reference.

Quote:
Probably I am missing something from the standard.

Yes.

Quote:
The only explanation I have is that if the temporary is not
referenced it
goes out of scope and gets deleted. If pointer is used, it is much
more
difficult (or even impossible) for the compiler to determine when the
temporary goes out of scope. Does this sound correct?

Pretty close.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ron Natalie
Guest





PostPosted: Wed Apr 27, 2005 8:48 am    Post subject: Re: Pointer to temporary Reply with quote

Dimitar wrote:


Quote:
const A* = &func();
// use A


Quote:

If reference is used instead of a pointer, everything is Ok.

Well the first problem is you have no identifier in the above declaration.

Yes, you are missing the fact that the temporary (the return of func)
ceases to exist at the end of the full expression it is created in.
In other words, by the time you get beyond "// use A" above it has
ceased to be.

However, there's a special feature for references that extends the
lifetime of any object bound to it until the reference itself ends.

const A& foo = func();
the returned value will stay valid as long as foo is.
If you want you can do
const A& foo = func(); // hold foo alive.
const A* fooptr = &foo; // and use foo as a pointer.

Pointers don't extend the life of temporaries because unlike references
they can be changed to refer elsewhere before their lifetime ends.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bronek Kozicki
Guest





PostPosted: Fri Apr 29, 2005 1:36 am    Post subject: Re: Pointer to temporary Reply with quote

Allan W wrote:
Quote:
Dimitar wrote (altered for brevity)

class A { /* ... */ };

void anotherFunc()
{
const A* = &func();

This is a syntax error. I think you meant
const A*a = &func();

this is still error and should not compile. Built-in operator& does not
accept temporary values. This limitation does not apply to overloaded
operator& , thus it will compile if there is one in class A . But then,
a is dangling pointer as you (and others) noted in this thread.


B.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dimitar
Guest





PostPosted: Sun May 01, 2005 2:59 pm    Post subject: Re: Pointer to temporary Reply with quote

Well,
it compiles without problems with gcc and microsoft C++ (.net).
gcc gives warning (even if you do not specify any flags), and the ms
compiler gives a warning only if you compile with warning level W4.

Probably the next question would be: Why does it compile?
Such code does not make sense.

Dimitar

Bronek Kozicki wrote:

Quote:
Allan W wrote:
this is still error and should not compile. Built-in operator& does not
accept temporary values. This limitation does not apply to overloaded
operator& , thus it will compile if there is one in class A . But then,
a is dangling pointer as you (and others) noted in this thread.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bronek Kozicki
Guest





PostPosted: Mon May 02, 2005 8:18 am    Post subject: Re: Pointer to temporary Reply with quote

Dimitar wrote:
Quote:
it compiles without problems with gcc and microsoft C++ (.net).
gcc gives warning (even if you do not specify any flags), and the ms
compiler gives a warning only if you compile with warning level W4.

Some compilers quite liberally implement rules of the C++ standard,
unless thay are told to be "strict". In MSVC use option /Za , in g++ use
-pedantic -ansi . But even with these flag g++ seems to ignore semantics
of builtin operator& .


B.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue May 03, 2005 8:21 am    Post subject: Re: Pointer to temporary Reply with quote

Bronek Kozicki wrote:
Quote:
Dimitar wrote:
it compiles without problems with gcc and microsoft C++
(.net). gcc gives warning (even if you do not specify any
flags), and the ms compiler gives a warning only if you
compile with warning level W4.

Some compilers quite liberally implement rules of the C++
standard, unless thay are told to be "strict". In MSVC use
option /Za , in g++ use -pedantic -ansi . But even with these
flag g++ seems to ignore semantics of builtin operator& .

Are you sure. I get a hard error from g++ when taking the
address of the return value of a function, both with a
relatively recent version (3.4.3) and with a very old one
(2.95.2). Presuming, of course, that the function returns a
value; if it returns a reference, everything compiles fine, as
it should.

--
James Kanze GABI Software
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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dimitar
Guest





PostPosted: Wed May 04, 2005 8:11 am    Post subject: Re: Pointer to temporary Reply with quote

Below is a small test program that I compiled and run. No errors, only
single warning.

Dimitar

bash-2.05b$ more Test.cpp
#include <iostream>

using namespace std;

class A
{
public:
A()
{
cout << "CONSTRUCTOR" << endl;
}
~A()
{
cout << "DESTRUCTOR" << endl;
}
};

A ff()
{
A a;
return a;
}

int main ()
{

const A& r = ff();
cout << "Point 1" << endl;
const A* p = &ff();
cout << "Point 2" << endl;

return 0;
}

bash-2.05b$ g++ -o Test Test.cpp
Test.cpp: In function `int main()':
Test.cpp:29: warning: taking address of temporary
bash-2.05b$ Test
CONSTRUCTOR
Point 1
CONSTRUCTOR
DESTRUCTOR
Point 2
DESTRUCTOR
bash-2.05b$ g++ --version
g++ (GCC) 3.3.5 (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:

Quote:
Bronek Kozicki wrote:

Are you sure. I get a hard error from g++ when taking the
address of the return value of a function, both with a
relatively recent version (3.4.3) and with a very old one
(2.95.2). Presuming, of course, that the function returns a
value; if it returns a reference, everything compiles fine, as
it should.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ben Hutchings
Guest





PostPosted: Thu May 05, 2005 12:16 am    Post subject: Re: Pointer to temporary Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
Quote:
Bronek Kozicki wrote:
Dimitar wrote:
it compiles without problems with gcc and microsoft C++
(.net). gcc gives warning (even if you do not specify any
flags), and the ms compiler gives a warning only if you
compile with warning level W4.

Some compilers quite liberally implement rules of the C++
standard, unless thay are told to be "strict". In MSVC use
option /Za , in g++ use -pedantic -ansi . But even with these
flag g++ seems to ignore semantics of builtin operator& .

Are you sure. I get a hard error from g++ when taking the
address of the return value of a function, both with a
relatively recent version (3.4.3) and with a very old one
(2.95.2). Presuming, of course, that the function returns a
value; if it returns a reference, everything compiles fine, as
it should.

The diagnostic varies depending on whether the type is a built-in or
user-defined type (reported as bug 21386).

Ben.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.