473,508 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple question: pointer to a local variable?

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
7 2634
"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

"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

"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
"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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2784
by: Asif | last post by:
Hi there, I have been trying to understand the behaviour of char (*pfn)(null) for a couple of days. can some body help me understand the behaviour of char (*pfn)(null) in Visual C++ environment?...
11
2545
by: kazack | last post by:
I am under the the impression that a variable is what is stored in a memory address and a pointer is the memory address of the variable? I was looking to use a function that does not return a...
4
2023
by: voidtwerp | last post by:
Hi, I hope this is not too OT but I would like clarification on how classes are held in memory. each object obviously has an individual copy of its data. I guess a class would only have one...
19
2441
by: mail1779205 | last post by:
I (certainly) hope I know what this function does: char *fun(void){ char *ptr = "Hello World"; return ptr; } It returns a pointer to a string stored somewhere in the memory and is...
4
1366
by: BSand0764 | last post by:
Apologies for the length of this message, but I'm having problems getting an alternate function to be executed via a functor implementation. I have two classes (BkgLand and BkgWater) that...
20
4006
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
12
2996
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
8
2189
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
3
1836
by: nembo kid | last post by:
I have an issue with a simple function that has to make a linear search for a key into an array. If the key is found in the array, the function it has to return 1 to the caller and pass array...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.