-
March 1st, 2011, 03:43 PM
#1
[RESOLVED] Subtle differences in std::vector
Hello,
I am studying subtle differences in the implementation of std::vector<T> for Microsoft and GCC compilers.
The program shown below gives different output for the two compilers. You need to look in the comments to see the outputs. I perfectly understand the output from GCC.
Output from GCC does this:
- CTOR 1: Creates one dummy on the stack for the vector's ctor
- Uses this dummy to initialize the vector
- DTOR 1: Deletes this dummy off the stack when vector's ctor is done.
- DTOR 2-3: vector's clear function deletes two dummys.
However, I don't understand why the Microsoft compiler has different output. I perused ISO/IEC 14882:2003 but could not find these implementation details.
- Shouldn't this stuff be specified?
- Why do these compilers seem to have implementation freedom?
Code:
#include <vector>
#include <iostream>
struct dummy
{
static unsigned ctor_count;
static unsigned dtor_count;
dummy()
{
++ctor_count;
std::cout << "C-TOR of dummy" << std::endl;
}
~dummy(void)
{
++dtor_count;
std::cout << "D-TOR of dummy" << std::endl;
}
};
unsigned dummy::ctor_count;
unsigned dummy::dtor_count;
int main(void)
{
std::vector<dummy> vd(2u);
vd.clear();
std::cout << "JUST AFTER CLEAR!" << std::endl;
std::cout << "Number of dummy ctors: " << dummy::ctor_count << std::endl;
std::cout << "Number of dummy dtors: " << dummy::dtor_count << std::endl;
}
/*
Visual Studio 2010
------------------
C-TOR of dummy
D-TOR of dummy
C-TOR of dummy
D-TOR of dummy
D-TOR of dummy
D-TOR of dummy
JUST AFTER CLEAR!
Number of dummy ctors: 2
Number of dummy dtors: 4
*/
/*
GCC 4.5.2
$ g++ -Wall -Wextra -pedantic -std=c++0x -O3 test.cpp -o test.exe
------------------
C-TOR of dummy
D-TOR of dummy
D-TOR of dummy
D-TOR of dummy
JUST AFTER CLEAR!
Number of dummy ctors: 1
Number of dummy dtors: 3
*/
You're gonna go blind staring into that box all day.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|