0

I executed the same code in compileonline.com and in Xcode, I got the wrong result in Xcode but correct result in compileonline.com

For the following code

#include <iostream> 
using namespace std;
int main()
{
    int n=0,i,x[n],y[n];

    // insert code here...
    cout << "Enter number of inputs\n";
    cin >> n;

    for (i=0;i<n;i++)
    {
        cin >> x[i];
        cin >> y[i];
    }
    //print x
    for ( i=0;i<n;i++)
    {
        cout << x[i];
    }

    cout << "Test1";

    //print y
    for ( i=0;i<n;i++)
    {
        cout << y[i];
    }
}

for http://www.compileonline.com/

Input :

1 5 6

Output :

Enter number of inputs

5Test16

for Xcode,

Input :

1 5 6

Output :

Enter number of inputs

6Test16

olevegard
  • 5,294
  • 1
  • 25
  • 29
  • 1
    arrays of size 0? This compiles? PS you change n after your arrays are initialized this is UB you'll be accessing outside the array if n grows after you initialize the arrays. PPS: as of now, variable length arrays are a gcc extension (aka not standard). – Borgleader Aug 29 '13 at 20:01

3 Answers3

1
int n=0,i,x[n],y[n];

This does not create valid arrays. If n is 0, then you get 0-sized arrays. Arrays of size 0 are not valid in C++, and so if your code even compiles, the behaviour is unpredictable. You want to later read n and create an array with this many elements, but you can't do it like this.

Instead, you should first read n, and then you can make the arrays like:

int *x = new int[n];
int *y = new int[n];
DUman
  • 2,560
  • 13
  • 16
  • 2
    In C++, we prefer `std::vector` over manually managed `int*`s. – fredoverflow Aug 29 '13 at 20:07
  • @FredOverflow In this case an `std::array` is probably even more appropriate due to the constant size, but while standard containers should be used, it's my belief that one should learn about manually managed arrays first. – DUman Aug 29 '13 at 20:14
  • @user1264727 `std::array` wouldn't work because the size isn't known at compile time. – David Brown Aug 29 '13 at 20:17
  • 2
    Bjarne believes that `std::vector` should be taught before arrays. As do many other C++ celebrities, Andrew Koenig and Barbara Moo being the most prominent examples. – fredoverflow Aug 29 '13 at 20:34
0

It looks like compileonline uses gcc and as far as I know Xcode uses either gcc or clang and both gcc and clang support flexible array members but as far as I can tell from the gcc document:

Flexible array members may only appear as the last member of a struct that is otherwise non-empty.

So this does not seem to be a valid use and if you change the code to explicitly use a 0 size and compile with -Wall -Wextra -pedantic you will see a message similar to this:

warning: zero size arrays are an extension [-Wzero-length-array]

So I am puzzled this works at all but you are surely invoking undefined behavior by accessing outside the array bounds and so you can not predict the results of this code. The correct and useful use for this would be something similar to this example from the gcc document:

struct line {
   int length;
   char contents[0];
};

struct line *thisline = 
     (struct line *) malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

It is weird that it works on compileonline.

Because your arrays x and y are of size 0. And in the loop you try to set the ith element of each array (which does not exist), then you have an undefined behaviour.

It is a good example of out-of-bounds error.

However, in cpp std::vector are preferred (in majority of cases) :

#include <vector>    // for std::vector

int n=0,i;
//       ^ No x[n] and y[n]

cout << "Enter number of inputs\n";
cin >> n;

std::vector<int> x(n);     // Create a vector x of size n
std::vector<int> y(n);     // Create a vector y of size n

Take a look at this live example with your input.

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62