By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,315 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Simple question: pointer to a local variable?

P: n/a
What's wrong with the following:

void SomeClass::someMethod(void)
{
A *ptrA = NULL;
ptrA = &A();

....
}

where A is some class type. It seems simple, but this is giving me
unpredictable behavior. Is it not just assigning ptrA to be a pointer
to a local variable? Doesn't that local variable then exist within
the scope of someMethod()?

TM
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"NS Develop" <ns*********@yahoo.com> wrote...
What's wrong with the following:

void SomeClass::someMethod(void)
{
A *ptrA = NULL;
ptrA = &A();
ptrA is assigned an address of a temporary variable that lives
only until the end of the expression it's used in (i.e. until
the closing semicolon). So, right after this statement 'ptrA'
is invalid.

....
}

where A is some class type. It seems simple, but this is giving me
unpredictable behavior.
In what sense?
Is it not just assigning ptrA to be a pointer
to a local variable?
No, there _is_ no variable. There is, OTOH, a temporary object.
Doesn't that local variable then exist within
the scope of someMethod()?


No, it does not.

Victor
Jul 22 '05 #2

P: n/a

"NS Develop" <ns*********@yahoo.com> wrote in message news:b4*************************@posting.google.co m...
What's wrong with the following:

void SomeClass::someMethod(void)
{
A *ptrA = NULL;
ptrA = &A();

....

You don't have a pointer to a local variable. You have a pointer to a temporary.
The temporary ceases to be at the end of the full expression (in this case the end
of the assignment statement).

The above shouldn't even compile. & requires an lvalue (or a qualified name).
Jul 22 '05 #3

P: n/a

"NS Develop" <ns*********@yahoo.com> wrote in message
news:b4*************************@posting.google.co m...
What's wrong with the following:

void SomeClass::someMethod(void)
{
A *ptrA = NULL;
ptrA = &A();

....
}

where A is some class type. It seems simple, but this is giving me
unpredictable behavior. Is it not just assigning ptrA to be a pointer
to a local variable?
No it's assigning the address of a temporary object.
There is no 'local variable' of type 'A'.
Doesn't that local variable then exist within
the scope of someMethod()?


The expression 'A()' creates a temporary object which
is destroyed immediately after the semicolon in your
assignment statement. So after that statement, the
value of the pointer object 'ptrA' is 'invalid', it
no longer points to an object.

-Mike
Jul 22 '05 #4

P: n/a
"emerth" <em****@hotmail.com> wrote in message
news:a9*************************@posting.google.co m...
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<6Duvb.204970$ao4.728848@attbi_s51>...
"NS Develop" <ns*********@yahoo.com> wrote...
What's wrong with the following:

void SomeClass::someMethod(void)
{
A *ptrA = NULL;
ptrA = &A();


ptrA is assigned an address of a temporary variable that lives
only until the end of the expression it's used in (i.e. until
the closing semicolon). So, right after this statement 'ptrA'
is invalid.


Please forgive a perhaps foolish question. I do not have very easy
access to a bookstore to buy a hardcore C++ reference work to look
this up in.

The statement "ptrA = &A();" creates something quite different
from, say, this:

{
A a_obj; // a local variable
A *ptrA = &a_obj;
// do some stuff with ptrA
} // now a_obj is invalid


Correct. As a matter of fact, as Victor pointed out, the
first form is not valid at all (taking address of a temporary).

Why would one use a temporary object? It doesn't seem very
useful.
It can sometimes be, especially as part of a larger
expression.
Is it possible to create a temporary object in this
manner because it can be useful, or because the compiler
sometimes has to for it's own reasons,
Yes, many expressions can cause creation of a temporary
object.
and so the syntax
is valid because of that?
The OP's syntax is not valid, your example is (but doesn't
really do anything useful.)
If a temporary object is useful to anyone other than the
compiler, how so?


Contrived example:

Suppose I want to output two items with a certain number
of space characters between them:

std::string s1 = "ABC";
std::string s2 = "XYZ";

std::cout << s1 << std::string(10, ' ') << 's2' << '\n';
/* prints "ABC XYZ" */

The expression std::string(10, ' ')
creates a temporary string object containing ten spaces.

After the statement, the temporary string is destroyed.

-Mike
Jul 22 '05 #5

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<QZ******************@newsread1.news.pas.eart hlink.net>...
"emerth" <em****@hotmail.com> wrote in message
news:a9*************************@posting.google.co m...
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<6Duvb.204970$ao4.728848@attbi_s51>...
"NS Develop" <ns*********@yahoo.com> wrote...


....
Contrived example:

Suppose I want to output two items with a certain number
of space characters between them:

std::string s1 = "ABC";
std::string s2 = "XYZ";

std::cout << s1 << std::string(10, ' ') << 's2' << '\n';
/* prints "ABC XYZ" */

The expression std::string(10, ' ')
creates a temporary string object containing ten spaces.

After the statement, the temporary string is destroyed.

-Mike


Mike:

You've opened my eyes to a thing, an idiom. Much appreciated!
Jul 22 '05 #6

P: n/a
Thank you all for your answers - it is clear to me what is going on now...

TM.

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<CJ*****************@newsread2.news.pas.earth link.net>...
"NS Develop" <ns*********@yahoo.com> wrote in message
news:b4*************************@posting.google.co m...
What's wrong with the following:

void SomeClass::someMethod(void)
{
A *ptrA = NULL;
ptrA = &A();

....
}

where A is some class type. It seems simple, but this is giving me
unpredictable behavior. Is it not just assigning ptrA to be a pointer
to a local variable?


No it's assigning the address of a temporary object.
There is no 'local variable' of type 'A'.
Doesn't that local variable then exist within
the scope of someMethod()?


The expression 'A()' creates a temporary object which
is destroyed immediately after the semicolon in your
assignment statement. So after that statement, the
value of the pointer object 'ptrA' is 'invalid', it
no longer points to an object.

-Mike

Jul 22 '05 #7

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<6Duvb.204970$ao4.728848@attbi_s51>...
"NS Develop" <ns*********@yahoo.com> wrote...
What's wrong with the following:

void SomeClass::someMethod(void)
{
A *ptrA = NULL;
ptrA = &A();


ptrA is assigned an address of a temporary variable that lives
only until the end of the expression it's used in (i.e. until
the closing semicolon). So, right after this statement 'ptrA'
is invalid.


Please forgive a perhaps foolish question. I do not have very easy
access to a bookstore to buy a hardcore C++ reference work to look
this up in.

The statement "ptrA = &A();" creates something quite different
from, say, this:

{
A a_obj; // a local variable
A *ptrA = &a_obj;
// do some stuff with ptrA
} // now a_obj is invalid

Why would one use a temporary object? It doesn't seem very
useful. Is it possible to create a temporary object in this
manner because it can be useful, or because the compiler
sometimes has to for it's own reasons, and so the syntax
is valid because of that?

If a temporary object is useful to anyone other than the
compiler, how so?

Many thanks,

Eric


....
}

where A is some class type. It seems simple, but this is giving me
unpredictable behavior.


In what sense?
Is it not just assigning ptrA to be a pointer
to a local variable?


No, there _is_ no variable. There is, OTOH, a temporary object.
Doesn't that local variable then exist within
the scope of someMethod()?


No, it does not.

Victor

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.