1

In dynamic allocation of arrays at runtime, when the array is declared before getting the array length, I encountered a behavior that could not explain. In the following code, the user provides the length of the array, say 5 and then provides five space-separated numbers as input, say 4 3 7 2 1. By printing length after the for loop we see that it changes. Here is the code.

#include <cstdio>
#include <iostream>
using namespace std;


int main() {
    int length;
    int ar[length];

    cout << "Enter the length of array: ";
     
    cin >> length;
    cout << "--- length before: " << length << endl;

    cout << "Provide " << length << " integer values separated by space and then press Enter." << endl;
    
    for (int i = 0; i<length; i++)
    {
        cin >> ar[i];
        cout << "i: " << i << " - ar[i]: " << ar[i] << endl;
    }
        
    cout << "--- length after: " << length << endl;
    

    return 0;
}

And the output with input 4 3 7 2 1:

Enter the length of array: 5
--- length before: 5
Provide 5 integer values separated by space and then press Enter.
4 3 7 2 1
i: 0 - ar[i]: 4
i: 3 - ar[i]: -1
--- length after: 4

After trying out different inputs, I found that length is changed to the first input integer, here 4. The value of i in the loop also jumps based on the given input. I know the array has to be declared after cin but any explanations for the behavior?

13
  • 4
    It's Undefined Behaviour. There is no explanation for what happens, compiler can do absolutely anything with such code. Commented Aug 19, 2024 at 14:40
  • 6
    Unless this is an academic exercise (homework) don't manually allocate arrays. Instead use a std::vector. Commented Aug 19, 2024 at 14:40
  • 3
    For some reason newbies always assume this will work, but it doesn't. The length of ar is fixed when you create it (and length is uninitialized at that time). The array will not track length and update its length dynamically. Commented Aug 19, 2024 at 14:43
  • 2
    If you are curious about how to use a vector instead (like you should), here's an example. Commented Aug 19, 2024 at 15:08
  • 4
    It seems to be a common belief among newbies that any program you write, and which compiles must have some defined behaviour, whatever that might be. There are languages that come close to that (Java for example) but C++ is a long long way away from that condition. You can easily write trivial C++ programs whose behaviour is undefined. When such programs run the compiler can (and will) make them behave in all sorts of strange and unpredictable ways. This is largely done for efficiency reasons, but it is one of the things that make C++ hard. Such is the price we pay for being C++ programmers. Commented Aug 19, 2024 at 15:17

1 Answer 1

3

After trying out different inputs, I found that length is changed to the first input integer, here 4.

What likely happens is that length happens to be zero (keep in mind that reading it is UB due to it being uninitialized, and that's not counting variable-length arrays not being a part of standard C++).

ar then becomes a zero-length array, and accessing a[0] is out of bounds (which is again UB). Apparently length happens to be right after the array in the stack, so it gets overwritten.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.