Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can sizeof(std::list<T>) vary for different types of T?

Tags:

c++

stl

Can I assume that for any type T, the type std::list<T> will have a same, constant size? Just to make it clear, I mean the size of the 'main' type itself only, not of the memory allocated by it.

It seems logical to me to assume that the size of T itself should only affect the size of list nodes allocated using the allocator.

However, there are two things that might cause the variancy of sizeof(std::list<T>) I can think of:

  1. A standard C++ library trying to 'optimize' the std::list type by putting some of T instances into the std::list<T> itself. That seems like a bad idea to me, and it probably breaks the 'constant time' requirements put by the standard;
  2. A standard C++ having library specializations of std::list<T> for some types, with the specializations having different sizes.

I can't think of any uses for (1) or (2) but I may be wrong.

What I'm thinking of achieving is using a fixed-size slice allocator for a template class usingstd::list<T> inside.


As a note: this is about variance for different types of T, and not for different allocators. I will have an explicit control of all instantiations, and they all will use the std::allocator.

like image 237
Michał Górny Avatar asked Aug 03 '12 11:08

Michał Górny


2 Answers

Yes, it can.

The standard does not precise any guarantee on the size of a list<T> object as far as I am aware. Still, there are other guarantees.


Let us debunk some things:

A standard C++ library trying to 'optimize' the std::list type by putting some of T instances into the std::list<T> itself. That seems like a bad idea to me, and it probably breaks the 'constant time' requirements put by the standard;

It does not break the 'constant time' requirement in any way as long as this number is bounded since O(5) is O(1). However, during a splice operations, the nodes should move from one list to another without moving in memory. Thus local storage is prohibited.

A standard C++ having library specializations of std::list<T> for some types, with the specializations having different sizes.

Since we already excluded the possibility of local storage, it is hard to imagine any other variations of the base std::list<T> type by itself.


And let us worry about what matters:

The memory allocated by a std::list<T> is provided by its second template argument: the allocator. Most allocators (including std::allocator<T> which is the default) use a simple pointer type: T*... however an allocator should feel free to change this type.

Therefore, by changing the allocator parameter (and more precisely, its pointer type), one will naturally change the size of the std::list container in all the implementations I have seen of it.

Note that the allocator itself could be specialized for some types, however with the rebind magic it's a bit more difficult to achieve.

like image 145
Matthieu M. Avatar answered Oct 07 '22 15:10

Matthieu M.


Chapter 23 of the C++11 standard (Containers) does not say anything about their sizes, so technically you cannot assume that the size will be constant. Implementors could (in theory) specialize some container for a specific primitive in a way that affects its footprint.

In practice, you could use a static assertion that sizeof(list<T>) == sizeof(list<int>) and use sizeof(list<int>) as the "fixed" size. Worst thing that can happen is a compile-time error.

like image 32
Jon Avatar answered Oct 07 '22 14:10

Jon