What are some limitations imposed by a fixed array size?

can you please tell me

A limitation of arrays is that they have fixed lengths. what does it means ?

It means they're allocated by the compiler when you compile the program, not when you run the program.

It means they're allocated by the compiler when you compile the program, not when you run the program.

Yes, Consider this example

#include <iostream>

using namespace std;

int main()
{

int numbers[5];//This is a fixed array of 5 memory slots.
cout<< "Enter  A Number";
cin >> numbers[0];//Filled Number 1
cout<< "Enter  A Number";
cin >> numbers[1]; // filled number 2
cout<< "Enter  A Number";
cin >>numbers[2];// filled number 3
cout<< "Enter  A Number";
cin >> numbers[3];//filled number 4
cout<< "Enter  A Number";
cin >> numbers [4];//filled number 5
cout<< "Enter  A Number";
cin >> numbers [5];/* Error:: No Space allocated (This is becoz it is the 6th number) And the compiler set a space for 5 numbers and the 6th number has no space ..
*/
for (int n=0; n<=6;n++)
{
cout<< number[n]<<"\n";
}

return 0;

}

Of course you can allocate memory from the heap during runtime so that the memory doesn't need to be allocated at compile-time, except then you would have to explicitly manage the memory...

Anyway, my point was that if you are going to explain the limitations of a fixed size array, you should understand the trade-off, or what you gain in return. The fixed size limitation is also the thing that makes an array so useful. The trade-off of dynamic (as opposed to fixed) length, which you would get from a data structure such as a list, is that you have constant-time, or O(1), access time. That is, you can index into any element of the array in constant time, no matter the size of the array (this has to do with the memory model, and how things are stored in memory, which I won't get into here...).

Yes, Consider this example

[....code]

cin >> numbers [5];/* Error:: No Space allocated (This is becoz it is the 6th number) And the compiler set a space for 5 numbers and the 6th number has no space ..
*/
for (int n=0; n<=6;n++)
{
cout<< number[n]<<"\n";
}

return 0;

}

Your for-statement if even worse, because it will try to access the 6th and 7th element. (it runs from 0 to 7). If you did this to prove your point, you might want to tell the OP about it ;)

Yes . With the vector you can eliminate that problem and have variable length arrays which you can create during run-time.

This article has been dead for over six months. Start a new discussion instead.