“STL Vector VS Static Array Vs Dynamic Array “

Ouasli Oumaima
2 min readDec 28, 2023

According to Bjarne Stroustrup , C++ language was design to :
• Be Compatible with C.
• Allow data abstractions .
• Enable object-oriented programming .

Java and C++ Differences :

[— JAVA — ]

Java allows the creation of objects by memory allocation.
Access to the object is by reference.
Destruction is done automatically by the crumb collector.

[ — CPP — ]

Allocation is done statically (global variable) in the stack (automatic variable or dynamically by heap.
Access to the objects is by pointer or reference.
Freeing the memory is done by the programmer in the case of dynamic allocation.

Static Array vs Dynamic Array vs STL Vector :

For Statics Arrays :It stores elements in subsequent memory “contiguous memory”.
It holds the same data types.
It has a fixed size.

✅Advantages:

Quick Access to values because they are available by using the name or the index of the element.

❌Disadvantages:

The size of the array is allocated at compile-time(Phase Of Translating The High-level code to machine code)and it must be declared in the declaration. That means that the size must be a constant expression and cannot be changed while the program is running.

For Dynamic Array :In Dynamic Arrays you can manage the memory by yourself and get a slight performance improvement but you risk to have a lots of bugs in the process.

For Vector :Dynamic Arrays are like a classic way of Vector.
Vector hides memory management ,it means the programmers will not handle every time ,the constructor or destructor memory management for the elements .Instead ,it provides to the programmer a lot of useful functionalities .

Vector is tested ,not error-prone, standardized and millions of developers know how to use it.

All in All, Vector is more better if you are going to change the size of your collection and insert/delete elements frequently.
But if you want just to acces to the elements frequently you can use the Static Array .

--

--

Ouasli Oumaima

🧑‍💻Software Engineering Student. 🔎A Data Enthusiast 📈