 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Marlene Miller Guest
|
Posted: Wed Jan 19, 2005 11:55 pm Post subject: what is a temporary object? |
|
|
I am trying to understand what a temporary object is by example.
string("a") + string("b)
Q1. Is the result of this expression a temporary object?
class C {
public:
operator int() { return 2; }
};
cout << C() << endl;
Q2. Is C() a temporary object?
Q3. A compiler might generate a temporary int object?
C f() { C c; return c; }
Q4. Is the object returned by f() a temporary object?
cout << &C() << endl;
cout << &f() << endl;
Q5. C() and f() are rvalues. & requires an lvalue. Why can we say &C() and
&f()? Is a temporary object involved?
[Except when performance is the issue, temporary objects rarely become the
concern of the programmer. TCPL 10.4.10 ]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Jan 20, 2005 12:20 pm Post subject: Re: what is a temporary object? |
|
|
* Marlene Miller:
| Quote: | I am trying to understand what a temporary object is by example.
string("a") + string("b)
Q1. Is the result of this expression a temporary object?
|
Yes.
| Quote: | class C {
public:
operator int() { return 2; }
};
cout << C() << endl;
Q2. Is C() a temporary object?
|
Yes, in this context.
| Quote: | Q3. A compiler might generate a temporary int object?
|
Yes.
| Quote: | C f() { C c; return c; }
Q4. Is the object returned by f() a temporary object?
|
Yes.
| Quote: | cout << &C() << endl;
cout << &f() << endl;
Q5. C() and f() are rvalues. & requires an lvalue. Why can we say &C() and
&f()? Is a temporary object involved?
|
This is invalid code, but some compilers (e.g. Visual C++) accept it
as non-standard extension -- enable the relevant warnings, if any.
| Quote: | [Except when performance is the issue, temporary objects rarely become the
concern of the programmer. TCPL 10.4.10 ]
|
Out of context that's a meaningless remark. There are two main reasons
why we generally care about temporary objects: efficiency and
correctness. And for the newbie also the issue of understanding what
goes on, e.g. understanding 'p++'. Neither efficiency nor correctness
is an issue with a simple type like 'int', but they both get more and
more relevant the larger the run-time and/or design-time cost of copying
an object is.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Thu Jan 20, 2005 6:06 pm Post subject: Re: what is a temporary object? |
|
|
Hi,
Marlene Miller wrote:
| Quote: | I am trying to understand what a temporary object is by example.
string("a") + string("b)
Q1. Is the result of this expression a temporary object?
|
Yes, because operator+ for strings returns by value.
This temporary object may be in some cases eliminated (depending on how
this operator is written and how you use its result).
| Quote: | class C {
public:
operator int() { return 2; }
};
cout << C() << endl;
Q2. Is C() a temporary object?
|
Yes.
| Quote: | Q3. A compiler might generate a temporary int object?
|
Yes.
| Quote: | C f() { C c; return c; }
Q4. Is the object returned by f() a temporary object?
|
Yes. It may be in some cases eliminated.
| Quote: | cout << &C() << endl;
cout << &f() << endl;
Q5. C() and f() are rvalues.
|
Yes.
| Quote: | & requires an lvalue.
|
Yes.
| Quote: | Why can we say &C() and
&f()?
|
We cannot.
But some compilers will accept it anyway.
| Quote: | Is a temporary object involved?
|
Yes.
| Quote: | [Except when performance is the issue, temporary objects rarely become the
concern of the programmer. TCPL 10.4.10 ]
|
Nevertheless, there are cases where temporary objects are not only of
concern, but may be also employed to perform useful work.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
s246794 Guest
|
Posted: Thu Jan 20, 2005 6:09 pm Post subject: Re: what is a temporary object? |
|
|
Marlene Miller wrote:
| Quote: | I am trying to understand what a temporary object is by example.
|
A function can return a pointer to an object on the heap, e.g.,
int*
f(void)
{
int* i = new int;
*i = 0;
return i;
}
But if I redefine `f()' as follows:
int
f(void)
{
int i = 0;
return i;
}
then `i' is an automatic variable and the memory it occupies is
recovered by the run-time system when the stack frame representing the
call to `f()' is popped from the stack after `f()' returns. That's why
the following redefinition causes `gcc' to issue a warning:
int*
f(void)
{
int i = 0;
return &i;
}
When I ran an example, and output the value, it was correct, but this is
a very risky thing to do. Here, `f()' is returning an address, and at
some point during execution, it's likely that other data will be written
to this address. `int i' will no longer exist after the stack frame for
a given invocation of `f()' is popped from the stack.
Using the second definition, where `f()' returns an `int', I could call
`f()' like this:
int a = f();
`f()' causes a temporary object of type `int' to be created. Here, it's
used as the rvalue in the assignment to `a'. But I can also choose to
ignore the return value of `f()', e.g.,
f();
Even in the first case, the memory allocated for the temporary may very
well not be used for `a'. So what happens to the temporary? Well, if
you have a smart compiler, it will "disappear", i.e., the memory it
occupies will be recovered. I think this will happen in any compiler
you're likely to be using.
I think it's important to consider where a temporary could be.
Practically speaking, and leaving aside registers and data files,
there are only three kinds of memory accessible to a programmer:
static (in the memory allocated to a running process corresponding to
the `data' segment of the executable), automatic, i.e., on the stack,
and dynamic, i.e., on the heap. It's not going to be in static memory,
or it wouldn't be temporary. I don't know where C++ compilers allocate
memory for temporaries, but I think it would work on either the stack or
the heap. If anyone knows better, I'd be quite interested to find out.
A good way to gain an insight into the way temporaries work is to write
a compiler (or interpreter) using a compiler generator, such as yacc or
GNU Bison. I use temporaries in my parser for GNU 3DLDF. They are
allocated on the heap and deleted in the parser rules when they're no
longer needed. I always delete them unless I use them as the semantic
value of the left-hand side symbol in the rule. I do this so that I
don't have to implement garbage collection. One could argue that this
is a form of garbage collection, but I think of it as preventing any
"garbage" from piling up in the first place.
Laurence Finston
http://www.gnu.org/software/3dldf/LDF.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|