PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

Free OpenACC Webinar

A Problem with the Compiler

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
Hush



Joined: 19 Mar 2012
Posts: 1

PostPosted: Mon Mar 19, 2012 1:30 pm    Post subject: A Problem with the Compiler Reply with quote

I have a simple test program like this:

#include<stdio>

int main(void)
{
int a[4] = {3,4,5,6};
int *p = a;
int i;

for(i=0; i<4; i++)
*p++ = *p + 10;

for(i=0; i<4; i++)
printf("a[%d]=%d\n", i, a[i]);

printf("a=%p, a+4=%p, p=%p\n", a, a+4, p);

return 0;
}

When I use pgcc 10.8-0 to compile and run it, I got this output:

a[0]=3
a[1]=14
a[2]=15
a[3]=16
a=0xbfabc7dc, a+4=0xbfabc7ec, p=0xbfabc7f6

a[0] isn't incremented. But when I use GCC to compile and run it, I got the correct output:

a[0]=13
a[1]=14
a[2]=15
a[3]=16
a=0xbf89c1ac, a+4=0xbf89c1bc, p=0xbf89c1bc

I think it's a bug in PGCC compiler.
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6208
Location: The Portland Group Inc.

PostPosted: Mon Mar 19, 2012 4:07 pm    Post subject: Reply with quote

Hi Hush,

All bets are off since with this code since the statement "*p++ = *p + 10;" will result in undefined behaviour. Since the order in which the left or right side evaluations are performed it is up to the implementer which "p" you're using and your results will vary by compiler and platform.

See: http://c-faq.com/expr/seqpoints.html

The fix is to break this into two lines:
Code:
$ cat test1.c
int main(void)
{
int a[4] = {3,4,5,6};
int *p = a;
int i;

for(i=0; i<4; i++) {
  *p = *p + 10;
   p++;
}

for(i=0; i<4; i++)
printf("a[%d]=%d\n", i, a[i]);

printf("a=%p, a+4=%p, p=%p\n", a, a+4, p);

return 0;
}
$ pgcc test1.c -fast
$ a.out
a[0]=13
a[1]=14
a[2]=15
a[3]=16
a=0x7fff1241d2a0, a+4=0x7fff1241d2b0, p=0x7fff1241d2b0


Hope this helps,
Mat
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group