29

With this code:

#include <iostream>
#include <list>

int main() {
    std::cout << sizeof(std::list<void*>) << std::endl;
};

I managed to notice that on GCC 4.7 the size of std::list<void*> on C++98 is 16 bytes, and its size on C++11 is 24 bytes.

I was wondering what changed on std::list that made it bigger.

5
  • 12
    From a technical standpoint, this behavior is completely implementation-specific. The library authors could, if they liked, decide arbitrarily to make the class 100x larger without any justification. It's probably not a good idea to write any code that depends on the size of a list being exactly 16 bytes; what were you doing where this actually caused a bug? Commented Apr 8, 2012 at 18:03
  • Take a look at here: en.cppreference.com/w/cpp/container/list You will see some member types have changed in c++11 in std::list. Commented Apr 8, 2012 at 18:04
  • 3
    They probably cached the list size. 24 bytes is three 64-bit values - a start pointer, an end pointer, and a size. Commented Apr 8, 2012 at 18:05
  • 4
    Without noticing, I was linking a C++98 lib with a C++11 application. This lib had a few inlines getter that were getting the wrong memory variable because of the different class memory layout the std::list caused. Commented Apr 8, 2012 at 18:05
  • @dexametason- I'm sorry, but I don't see any change to the interface that would force the implementation to use a larger size. Can you point out specifically what it was that you're referring to? Commented Apr 8, 2012 at 18:06

1 Answer 1

46

C++11 requires list::size() to execute in constant time. GCC made this possible by adding the size as a data member. GCC did not do so for C++98 mode, because that would break binary compatibility.

Don't mix code compiled in C++98 mode with code compiled in C++11 mode. It doesn't work.

Update: apparently, the GCC folks had a change of heart, and C++11 conformance is less important than maintaining compatibility for now, so list::size() will no longer execute in constant time in GCC 4.7.2. It will in a future version, in both C++98 and C++11 modes.

Sign up to request clarification or add additional context in comments.

15 Comments

What is the complexity of std::list<T>::splice, now?
@André : It depends which overload you call. Some overloads are O(1), some are O(N).
You need to update the size field, and so you need to count the amount of items being referenced by the iterators.
The two and three argument splices are constant time, the four argument version (iterator range on the from list) is linear in the iterator range distance unless &from == this in which case it's constant time.
@emsr That's what the standard requires, did you check that or did you also check whether GCC's implementation conforms in that regard?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.