Bug 27744 - Optimized code gives incorrect result
Summary: Optimized code gives incorrect result
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.0
: P3 major
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2006-05-23 19:20 UTC by Orion Poplawski
Modified: 2006-05-23 20:42 UTC (History)
60 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Orion Poplawski 2006-05-23 19:20:31 UTC
The following code:

#include <stdio.h>

int main(int argc, char **argv) {
  void *v = (void*) 1;
  int *i = (int*) &v;
  int intpos;

  for (intpos = 0; intpos < (sizeof(void*) / sizeof(int)); intpos++)
    if (i[intpos] == 1)

  printf("intpos = %d\n",intpos);


intpos = 0

when compiled optimization of -O or less.  When compiled with -O2, it produces:

intpos = 1

on x86 and:

intpos = 2

on x86_64.

Latest gcc tested is gcc version 4.1.0 20060512 (Red Hat 4.1.0-17)

This code is from LAM/MPI.
Comment 1 Falk Hueffner 2006-05-23 19:40:32 UTC
This code accesses v, which is of type void*, via an lvalue of type int.
This is not allowed by C aliasing rules. Use -fno-strict-aliasing or a
union to express this operation.
Comment 2 Andrew Pinski 2006-05-23 20:41:50 UTC
Reopening to ....
Comment 3 Andrew Pinski 2006-05-23 20:42:24 UTC
to mark this as a dup of bug 21920.

*** This bug has been marked as a duplicate of 21920 ***