Simple question: pointer to a local variable?

N

NS Develop

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
 
V

Victor Bazarov

NS Develop said:
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
 
R

Ron Natalie

NS Develop said:
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).
 
M

Mike Wahler

NS Develop said:
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
 
E

emerth

Victor Bazarov said:
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
 
M

Mike Wahler

emerth said:
"Victor Bazarov" <[email protected]> wrote in message

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
 
E

emerth

Mike Wahler said:
....

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!
 

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
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top