• Register
1,240 points
10 7 4

Array of length n in C++

posted Jul 17, 2020 1 min read

An array is a collection of items stored at contiguous memory locations. Although we have to define the size of the array before the time of the compilation. Wouldn't it be cool if I didn't have to define the size before the compilation. In this article we will discuss this topic.

 

Unfortunately, we cannot define the variable size of an array since the size of the array has to be known before the compilation. In other words, variable length arrays are not allowed in C++. Maybe the developers of the C didn’t think it was necessary although it will be a great feature. But since the language C++ itself has evolved from the language C. So, it has some limitations or we can say C++ simply inherits the features of the C.
 

Alternative

But we don’t have to worry about this lack of feature since it is not lacking from the C++. We have vectors for VLA(variable length array). Vectors use dynamic memory allocation for memory allocation. So instead of using an array of length n, you should use vectors.

 

Hope this article helps you. If you have any kind of issue, feel free to ask.