Remi.b
Remi.b

Reputation: 18229

Ensure a `std::vector` won't move its pointer

In C++, a std::vector<T> is basically a pointer (T*), a size object (int) and a capacity object (int).

At construction, std::vector creates a new pointer T* and as the vector grows, std::vector is responsible for allocating the needed memory as the vector grows. If the vector grows so that there is no more room to add elements T, then std::vector will automatically move the pointer to another location and copy the data over.

Is it possible to tell the vector what pointer to use at construction and indicate a fix size and abort if program attempts to make the vector bigger than the size indicated at construction time?

Upvotes: 0

Views: 96

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490328

No, std::vector doesn't provide a direct way of doing that.

std::vector allocates space via an allocator object though. If you wanted to, it would be fairly easy to write a minimal allocator that had an extra call to disable allocation and call abort if allocation were attempted after it was disabled.

Upvotes: 2

Related Questions