Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does std::list have a max size?

Tags:

c++

If std::list is a linked list then why is there a limit on how many elements you can have? Each element is a link to a new node and there's no limit on how many pointers you can have.

like image 638
template boy Avatar asked Dec 16 '14 16:12

template boy


People also ask

Should I use std::list?

Consider using std::list if: You need to store many items but the number is unknown. You need to insert or remove new elements from any position in the sequence. You do not need efficient access to random elements.

Is std::list linked?

The std::list is implemented as a doubly-linked list.

What is Max_size C++?

The set::max_size() is a built-in function in C++ STL which returns the maximum number of elements a set container can hold. Syntax: set_name.max_size() Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a set container can hold.


2 Answers

If std::list is a linked list then why is there a limit on how many elements you can have?

Because the max_size() function is a requirement for all standard containers.

Each element is a link to a new node and there's no limit on how many pointers you can have.

Yes there is: the size must be representable by size_type, so a limit is that type's maximum value. There's probably no reason for it to be less than that.

like image 130
Mike Seymour Avatar answered Nov 03 '22 01:11

Mike Seymour


there's no limit on how many pointers you can have

There's a limit on how many distinct pointer values there can possibly be, based on the size of a pointer. For example, if a pointer occupies 64 bits in a particular implementation, then max_size() could safely return 264-1. In fact it could be rather less than this, since each linked list node will be bigger than 1 byte.

like image 34
Steve Jessop Avatar answered Nov 02 '22 23:11

Steve Jessop