user2730651
user2730651

Reputation: 1

Different results in different c++ IDE

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

Upvotes: 0

Views: 152

Answers (3)

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

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.

Upvotes: 0

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158529

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;

Upvotes: 0

DUman
DUman

Reputation: 2590

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];

Upvotes: 1

Related Questions