473,231 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 2618
"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
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
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
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
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
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
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
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
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
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.