0

I have the following code:

#include <iostream>

using namespace std;

int main()
{
    int length = 0;
    int arrA[length];

    cout << "Enter the length : ";
    cin >> length;

    cout << "Enter " << length << " integers for array : ";

    for(int i = 0; i < length; i++)
    {
        cin >> arrA[i];
    }

    cout << "Array A : ";
    for(int i = 0; i < length; i++)
    {
        cout << arrA[i] << " ";
    }
    cout << endl;
}

The above require the user to input the length of array followed by the integers that will be store in array. It work but the printing of the array value is incorrect when I input length 8 and above.

Working

Enter the length : 7
Enter 7 integers for array : 7 6 5 4 3 2 1
Array A : 7 6 5 4 3 2 1

Not working

Enter the length : 8
Enter 8 integers for array : 8 7 6 5 4 3 2 1
Array A : 8

Could this be due to memory issue or something?

user3118602
  • 553
  • 5
  • 19

3 Answers3

2
int length = 0;
int arrA[length];

This creates an array of size zero

cout << "Enter the length : ";
cin >> length;

This sets the length variable to a value entered by the user but doesn't change the size of arrA which has already been created.

You obviously think that when the length variable changes that the array arrA will change as well, but this is not true.

Because you are accessing elements of an array which has zero size the behaviour of your program is undefined.

In addition this whole approach is incorrect because

int length;
...
int arrA[length];

is not legal C++. Because length is a variable this is a variable length array or VLA. VLAs are legal in C but are not legal in C++.

The best way to write this code in C++ is to use a vector. A vector is the C++ improvement on a C array.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int length;

    cout << "Enter the length : ";
    cin >> length;

    vector<int> arrA(length);
    ...
john
  • 85,011
  • 4
  • 57
  • 81
  • Hi, thanks for the explanation. I am from Python background, guess this is bad practise. I am avoiding using vector. What is the non-vector way if you don't mind editing your post? – user3118602 Jul 11 '20 at 08:12
  • The non-vector way is `int* arrA = new int[length];`. There's no reason to get into bad habits however. – john Jul 11 '20 at 08:17
  • Why are you avoiding vector? Have you been told you must? Otherwise it makes no sense. – john Jul 11 '20 at 08:17
  • I am learning basic C++ now in my educational and want to avoid using vector to understand how certain things work. – user3118602 Jul 11 '20 at 08:21
  • It's best to use @john's example by using a vector. You can check out my answer that uses a pointer - but again, it's just bad practice. – Geno C Jul 11 '20 at 08:32
  • @user3118602 Pointers (which is what my second example is using) are one of the most complex features of introductory level C++. It makes sense to avoid learning about them while you still have everything else to learn. Using vector and similar will ease your way to mastering C++. So goes the argument anyway. – john Jul 11 '20 at 08:46
2
int length = 0;
int arrA[length];

This is not valid C++. What you may want to do is:

int *arrA = new int[length];

after cin >> length. You will have to free the pointer later with delete[] arrA;

H Krishnan
  • 896
  • 9
  • 12
1

It is not due to a memory issue. But because variable length arrays are not part of the C++ standard. length is a variable length array. Read more about variable length arrays
Here

Here is the code if you do not want to use a vector. I would suggest not to use this for multifarious reasons. But go ahead and knock yourself out.

#include <iostream>

using namespace std;

int main()
{
    int length = 0;

    int *arrA = new int [length];

    cout << "Enter the length : ";
    cin >> length;
   
    cout << "Enter " << length << " integers for array : ";

    for (int i = 0; i < length; i++)
    {
        cin >> arrA[i];
        
    }
   

    cout << "Array A : ";
    for (int i = 0; i < length; i++)
    {
        cout << arrA[i] << " ";
    }
    
    delete[] arrA;
    cout << endl;
    return 0;
}
Geno C
  • 1,401
  • 3
  • 11
  • 26