Software Archive
Read-only legacy content
17061 Discussions

A strange behavior in array operations

Hanfeng_C_
Beginner
414 Views

Hello,

When I played with array operations in Cilk Plus, I found the same assignment might return different results with gcc and g++.  For example, in array[0:5] = array[10:5], we expect that five elements 10 to 14 are copied to elements 0 to 4 in order.  There is no overlap in the example.  It works when it is compiled with g++.  However, it goes awry when it is compiled with gcc.  Actually, another five elements 10 to 6 are copied.

Is it an error or a bug?  Thanks.

Configuration:

         gcc (Ubuntu 5.3.0-3ubuntu1~14.04) / g++ (Ubuntu 5.3.0-3ubuntu1~14.04)

Code:  arr.c

#include <stdio.h>
#include <cilk/cilk.h>

int main()
{
	int a[99];
	int n = 20;
	for(int i=0;i<n;i++)
		a[i] = i + 1;
	a[0:5] = a[10:5]; //<--------
	for(int i=0;i<n;i++)
		printf("a[%d] = %d\n",i,a);
	return 0;
}

Compile with gcc:

         gcc arr.c -fcilkplus -lcilkrts​

./a.out
a[0] = 11
a[1] = 10
a[2] = 9
a[3] = 8
a[4] = 7
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10
a[10] = 11
a[11] = 12
a[12] = 13
a[13] = 14
a[14] = 15
a[15] = 16
a[16] = 17
a[17] = 18
a[18] = 19
a[19] = 20

Compile with g++:

         g++ arr.c -fcilkplus -lcilkrts​

./a.out
a[0] = 11
a[1] = 12
a[2] = 13
a[3] = 14
a[4] = 15
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10
a[10] = 11
a[11] = 12
a[12] = 13
a[13] = 14
a[14] = 15
a[15] = 16
a[16] = 17
a[17] = 18
a[18] = 19
a[19] = 20

 

0 Kudos
2 Replies
Pablo_H_Intel
Employee
414 Views

Wow, that is really strange.  It does appear that there is a serious bug in the C compiler (gcc) but not the C++ compiler (g++).  Does the same thing happen if you use two separate arrays (i.e., a[0:5] = b[10:5])?  (I'm guessing no.)

0 Kudos
Hanfeng_C_
Beginner
414 Views

Yes, the result of two separate arrays is still the same.  It seems that gcc decreases the index while g++ increases the index on the RHS.

Initially, the code was saved in arr.c.  After I change it to arr.cpp, both gcc and g++ produce correct output.  The only combination fails is arr.c + gcc

0 Kudos
Reply