-4

This is probably something that can be easily solved. I have no idea what I am doing wrong, please help with this super simple code.

#include <iostream>

using namespace std;

int main()
{
    int arraysize, i, a[arraysize], n, j;

    cout << "array size";
        cin >> arraysize;

    for(int i=0, j = arraysize; i < arraysize, j > 0; i++, j--){
        a[i] = j;
        cout << "a[" << i << "] = " << a[i] << endl;
    }
}

When executing this program, it will output 5 values of the array, and then I get a segmentation fault. I have no idea how to fix this and\or how to properly execute this type of problem. Any help is appreciated, thank you.

3141
  • 401
  • 1
  • 6
  • 23
  • `arraysize` holds garbage value at the time you are declaring the array `a[arraysize]` . – Aditi Rawat Dec 29 '17 at 19:43
  • [Could not](http://coliru.stacked-crooked.com/a/301743aa2694a77a) reproduce with GCC. Don't use VLAs and initialize your variables prior to using them. – Ron Dec 29 '17 at 19:44
  • As @AditiRawat says, `arraysize` holds garbage at the point where it's first used. Further, `int a[arraysize]` is not valid C++, even if there is a valid value in `arraysize`. Some compilers allow this as an extension, but should give you at least a warning that what you're doing isn't kosher. – Pete Becker Dec 29 '17 at 19:46

2 Answers2

3

arraysize has no specified value when a is constructed, which means you've invoked undefined behavior in specifying the size of a.

The non-undefined behavior version of this code is this:

#include <iostream>

//Don't use "using namespace std;"!
//using namespace std;

int main()
{
    //removed unused/unnecessary variables
    int arraysize;

    std::cout << "array size";
        std::cin >> arraysize;

    int a[arraysize];

    for(int i = 0; i < arraysize; i++) {
        int j = arraysize - i;
        a[i] = j;
        std::cout << "a[" << i << "] = " << a[i] << std::endl;
    }
}

HOWEVER. Specifying an array size at runtime like this is a non-standard extension to the language, and may not work in all environments. If you want this code to be standard-compliant, you need to use std::vector.

#include <iostream>
#include <vector>

int main()
{
    int arraysize;

    std::cout << "array size";
        std::cin >> arraysize;

    std::vector<int> a(arraysize);

    for(int i = 0; i < arraysize; i++) {
        int j = arraysize - i;
        a[i] = j;
        std::cout << "a[" << i << "] = " << a[i] << std::endl;
    }
}

I've removed the use of using namespace std; from your code, for these reasons.

Xirema
  • 19,889
  • 4
  • 32
  • 68
0

This is clearly undefined behavior.
First of all C++ standard do not support variable length array. This is extension of some compilers (like gcc).

Secondly value of arraysize is undefined when you instantiated an array, so result is unpredictable it may work in some cases, but this is based on pure luck.

If you need dynamic size array it is best o use std::vector.

#include <iostream>

using namespace std;

int main()
{
    int arraysize, i, n, j;
    std::vector<int> a;

    cout << "array size";
    cin >> arraysize;

    std::vector<int> a(arraysize);

    for(int i=0, j = arraysize; i < arraysize, j > 0; i++, j--){
        a[i] = j;
        cout << "a[" << i << "] = " << a[i] << endl;
    }

    return 0;
}
Marek R
  • 32,568
  • 6
  • 55
  • 140