0

I was messing around just trying to understand how c++ works, when I got "crashed" by the int arrayF[0]; on line 6, and int arrayF[input]; on line 21. In the computer memory the first version of this arrayF shouldn't be overwrote by the one on line 21? I know that int arrayF[input]; is in another scope, but still do what is supposed to do, or not?

And I tested another thing(don't know why) removing the int from int arrayF[input]; on line 21 like I was trying to access that address and somehow, the program works as intended. Why? This should arise a error by trying to access a unexistent address.

Yes, this a fibonacci program, my intention is not make this exactualy right, I'm just messing with the language.

#include <iostream>
#include <string>

using namespace std;

int arrayF[0];
int fiboR(int i, int n)
{
        if(i == n) return arrayF[n];
        else
                arrayF[i + 2] = arrayF[i] + arrayF[i + 1];
                return fiboR(i + 1, n);
}

int main(int argc, char *argv[])
{
        int input = stoul(argv[argc - 1]);
        int start = stoul(argv[argc - 2]);

        int arrayF[input];
        arrayF[0] = 0;
        arrayF[1] = 1;

        cout << fiboR(start, input) << "\n";

        return 0;
}

The program works by calling it with command-line arguments, the 0 is the first fibonacci's sequence index, a start argument. The next numer is the desired index of the fibonacci's sequence

Output with int arrayF[input]; on line 21:

$ ./a.out 0 10

0

Output with arrayF[input]; on line 21:

$ ./a.out 0 10

55

Natan
  • 3
  • 3
  • Arrays with dynamic bounds are supported by some compilers as an extension, but are not actually C++. – Sebastian Redl Oct 01 '21 at 11:35
  • 1
    `int arrayF[0];` is an array with 0 elements. That's OK if you use it as a function argument (where every array decays to a pointer anyway) but not when you allocate storage (like in a global or local variable). – Scheff's Cat Oct 01 '21 at 11:35
  • `int arrayF[input];` (with `input` a non-const value obtained at run-time) is a so-called [VLA](https://en.wikipedia.org/wiki/Variable-length_array). This is optional in C standard but not in C++. Please, use a [std::vector](https://en.cppreference.com/w/cpp/container/vector) instead (which is the proper C++ tool for this). – Scheff's Cat Oct 01 '21 at 11:37
  • 1
    And `int arrayF[input];` is a [variable-length array](https://en.wikipedia.org/wiki/Variable-length_array) and they [aren't actually part of C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use `std::vector` instead. – Some programmer dude Oct 01 '21 at 11:37
  • 2
    And as a side note, "just messing with the language" doesn't work in C and C++: they don't necessarily tell you when you're doing something wrong. – Sebastian Redl Oct 01 '21 at 11:38
  • Furthermore, for an array (or a vector) of `n` elements, then `n` as an index will be out of bounds. You might want to think about that in regards to `return arrayF[n]` – Some programmer dude Oct 01 '21 at 11:38

1 Answers1

0
int arrayF[0];

This program is ill-formed. The size of an array variable must not be zero.

if(i == n) return arrayF[n];

Even if we pretend that empty array variables were allowed, then all indices to such array would be outside the bounds of the array. Regardless of what the value of n is, this would read outside the bounds and behaviour of the program will be undefined.

int arrayF[input];

The size of an array must be compile time constant. input is not compile time constant. The program is ill-formed.

Note that this second array variable is local to the main function. The fiboR function is outside of that scope and it will not see this variable.

memory the first version of this arrayF shouldn't be overwrote by the one on line 21?

That's not how C++ works. The both arrays exist in different parts of the memory. The name within the nested scope hides the other name within the inner scope - but not outside of its scope.

And I tested another thing(don't know why) removing the int from int arrayF[input]; on line 21 like I was trying to access that address and somehow, the program works as intended. Why? This should arise a error by trying to access a unexistent address.

The behaviour of accessing outside of bounds is undefined. There is no guarantee that you would get an error even though you may hope to get one. Evidently you weren't lucky this time.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Like I said, the intention was to play with things. I was wondering about the scope as you described and thanks for the confirmation! And I didn't know about the `std::vector` in C++ instead of arrays like in other languages. – Natan Oct 01 '21 at 12:55