5

std::deque is contiguous memory container or not ?

The famous book Effective STL by Scott Meyers says like below

Contiguous-memory containers (also known as array-based containers] store their elements in one or more (dynamically allocated) chunks of memory, each chunk holding more than one container element. If a new element is inserted or an existing element is erased, other elements in the same memory chunk have to be shifted up or down to make room for the new element or to fill the space formerly occupied by the erased element. This kind of movement affects both performance (see Items 5 and 14) and exception safety (as we'll soon see). The standard contiguous-memory containers are vector, string, and deque. The nonstandard rope is also a contiguous-memory container.

But you can find opposite explanation in cppreference.com

As opposed to std::vector, the elements of a deque are not stored contiguously: typical implementations use a sequence of individually allocated fixed-size arrays, with additional bookkeeping, which means indexed access to deque must perform two pointer dereferences, compared to vector's indexed access which performs only one.

Which one is true ?

myoldgrandpa
  • 871
  • 7
  • 21

1 Answers1

7

There is no conflict between the two quotes. Scott uses the term "contiguous container" in a sense a little wider than you might have seen it used elsewhere.

Scott writes (emphasize mine):

Contiguous-memory containers (also known as array-based containers] store their elements in one or more (dynamically allocated) chunks of memory, [...]

As the quote from cppref states, std::deque uses multiple arrays to store the elements. Within each array the elements are contiguous. Scott does not claim that all elements in a std::deque are contiguous in one chunk of memory.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    I would say inconsistent or not pressie terminology is problem here. – Marek R Apr 07 '22 at 14:44
  • spoken language is necessarily imprecise. Scott puts the definition of the term he uses first so there is no issues with his text. You cannot blame him for other texts using slightly different terms and meanings ;) – 463035818_is_not_an_ai Apr 07 '22 at 14:45
  • Nevertheless I would say that Meyers misuses the term "contiguous" here. It has a well defined meaning for containers and iterators in the standard where it means as much as "in one chunk". – Jakob Stark Apr 07 '22 at 14:48
  • I'm not blaming anyone. I'm just saying that there is no formal definition (maybe I will try find one) then this kind confusion is possible. First quote is better since provides definition. – Marek R Apr 07 '22 at 14:48
  • @JakobStark indeed. If you allow chunks of single elements, then every container is made of chunks of contiguous memory :P Though scott is rather explicit in excluding that – 463035818_is_not_an_ai Apr 07 '22 at 14:49
  • Thanks for clear answer. But it's quite a misleading sentence. – myoldgrandpa Apr 07 '22 at 15:00
  • 2
    @myoldgrandpa • Consider a `vector`. Each `string` stores the `char` in a contiguous chunk of memory. But the chunks are not contiguous for `vector`. A `deque` is like that; it has scattered non-contiguous chunks, and within the chunks the objects are contiguous. – Eljay Apr 07 '22 at 15:07