Skip to content
rbourgeois33Public

About

My non-exhaustive implementation of `std::vector` for learning's sake

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

23 Commits

Folders and files

Repository files navigation

vector

My non-exhaustive implementation of std::vector in vector.h for learning's sake. Some std::algorithms threw at it in algorithms.cpp.

Generative AI usage acknowledgement

Almost no AI in vector.h (except small identified portions), while tests.cpp is fully AI-generated for the exercise's sake.

C++ Lessons I learned / solidified along the way

Vector's state rule (class invariants !) to keep in mind

  1. size_<=capacity_.
  2. v[i] for i in [0, size_-1] must be initialized (not just malloc-ed bytes but real T's constructed).
  3. v[i] for i in [size_,capacity_-1] must be not initialized, so freed or never initialized.
  4. data_==nullptr <--> capacity_=0
  5. if capacity_>0, data_ is a pointer to a malloced piece of capacity_*sizeof(T) bytes.

One of the main challenge is that these rules should hold even in exception are thrown by the various ctor/dtor called in the vector's methods.

Aliasing in push_back, emplace_back

Both push back and emplace back can have an aliasing problem if the argument passed is a reference to an element of the vector an a reallocation is needed.

general useful concepts

About

My non-exhaustive implementation of `std::vector` for learning's sake

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages