This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Temporary object omits constructor
- From: Stefan Schulze Frielinghaus <stefan at seekline dot net>
- To: gcc at gcc dot gnu dot org
- Date: Wed, 21 May 2008 19:14:27 +0200
- Subject: Temporary object omits constructor
Hello all,
I just tried to easily show how many temporary objects get created in my
program. I created a test application like this one:
#include <iostream>
struct A {
static int c, d;
A() { ++c; }
~A() { ++d; }
void operator+=(const A&) { }
A operator+(const A&) { return *this; }
};
int A::c = 0;
int A::d = 0;
int main() {
A a, b, c;
a = b + c;
//a = b;
//a += c;
std::cout << A::c << " " << A::d << std::endl;
}
Whenever an object of class type A is created a counter is incremented.
So when I compile this application
(g++ -Wall -Wextra -O0 -ggdb2 -o test test.cpp) and run it I suspected
to get an output like this one: 4 1 (three objects plus one temporary
which gets destructed before std::cout).
But the actual result was: 3 1
The assembler output (i386) looks like this (without iostream etc):
gdb disas main:
0x080483d4 <main+0>: lea ecx,[esp+0x4]
0x080483d8 <main+4>: and esp,0xfffffff0
0x080483db <main+7>: push DWORD PTR [ecx-0x4]
0x080483de <main+10>: push ebp
0x080483df <main+11>: mov ebp,esp
0x080483e1 <main+13>: push ecx
0x080483e2 <main+14>: sub esp,0x24
0x080483e5 <main+17>: lea eax,[ebp-0x6]
0x080483e8 <main+20>: mov DWORD PTR [esp],eax
0x080483eb <main+23>: call 0x804845c <A>
0x080483f0 <main+28>: lea eax,[ebp-0x7]
0x080483f3 <main+31>: mov DWORD PTR [esp],eax
0x080483f6 <main+34>: call 0x804845c <A>
0x080483fb <main+39>: lea eax,[ebp-0x8]
0x080483fe <main+42>: mov DWORD PTR [esp],eax
0x08048401 <main+45>: call 0x804845c <A>
0x08048406 <main+50>: lea edx,[ebp-0x5]
0x08048409 <main+53>: lea eax,[ebp-0x8]
0x0804840c <main+56>: mov DWORD PTR [esp+0x8],eax
0x08048410 <main+60>: lea eax,[ebp-0x7]
0x08048413 <main+63>: mov DWORD PTR [esp+0x4],eax
0x08048417 <main+67>: mov DWORD PTR [esp],edx
0x0804841a <main+70>: call 0x8048480 <_ZN1AplERKS_>
0x0804841f <main+75>: sub esp,0x4
0x08048422 <main+78>: lea eax,[ebp-0x5]
0x08048425 <main+81>: mov DWORD PTR [esp],eax
0x08048428 <main+84>: call 0x804846e <~A>
0x0804842d <main+89>: lea eax,[ebp-0x8]
0x08048430 <main+92>: mov DWORD PTR [esp],eax
0x08048433 <main+95>: call 0x804846e <~A>
0x08048438 <main+100>: lea eax,[ebp-0x7]
0x0804843b <main+103>: mov DWORD PTR [esp],eax
0x0804843e <main+106>: call 0x804846e <~A>
0x08048443 <main+111>: lea eax,[ebp-0x6]
0x08048446 <main+114>: mov DWORD PTR [esp],eax
0x08048449 <main+117>: call 0x804846e <~A>
0x0804844e <main+122>: mov eax,0x0
0x08048453 <main+127>: mov ecx,DWORD PTR [ebp-0x4]
0x08048456 <main+130>: leave
0x08048457 <main+131>: lea esp,[ecx-0x4]
0x0804845a <main+134>: ret
Only three times the constructor is called but four times the destructor
and a magic call to "ï0x8048480 <_ZN1AplERKS_>".
Can someone explain these results?
Best regards
Stefan