Search code examples
cosx-snow-leopardvalgrindicc

Unstable output when using icc


I would like to report an intriguing bug I have. The piece of code below is supposed to print out 20 times "1.0". Instead, when compiling with icc (11.1) on my mac (snow leopard 10.6.8), I get unstable values (16 times "0.0" then 4 times "1.0"). I make use of several features in the code but none of them seems to have a bad syntax (no error during compilation, and valgrind reports no error during running). However if I change anything (even non used function - that's why I find it very strange), I get the correct output. Compiling with gcc gives the correct output as well.

But I think the strangest thing is that if I delete the function "function1", the bug disappears, although the function is NOT used in the code.

This is really odd, and now I fear that my code (which is much bigger than that) will be unstable. I need your help, I'm really puzzled by this. Is there anything wrong in the syntax?

main.c:

#include "main.h"

 int main(argc,argv)
 int argc;
 char **argv;
{

  Config para;
  para.option1 = ONE;

  para.a[0] = 0.0;
  para.a[1] = 0.0;
  para.a[2] = 0.0;
  para.a[3] = 1.0;

  int i;  
  double *x = (double *)malloc(20*sizeof(double));
  for(i=0;i<20;i++) x[i] = 1.0;
  for(i=0;i<20;i++) printf("%f \n", x[i]);
  free(x);

  function2(para);

  return EXIT_SUCCESS;
}

void function1(int option){

  switch(option){
  case ONE: case TWO: case THREE: case MONE:
    printf("MONE to THREE\n");
    break;
  case FOUR:
    printf("FOUR\n");
    break;
  }

  return;
}

void function2(const Config para){

  if(para.option1 == FOUR){
    printf("FOUR\n");
  }

  return;
}

main.h:

#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <stdarg.h>

#define MONE    -1
#define ONE      1
#define TWO      2
#define THREE    3
#define FOUR     4

typedef struct Config
{
  int option1, option2;
  double a[4];
} Config;

void function1(int option);
void function2(const Config para);

Solution

  • When digging more on the web, I found this bug report from Intel:

    http://software.intel.com/en-us/articles/intel-compiler-and-xcode-322-linker-runtime-crash-with-switch-statement/

    It seems to be related to how the icc compiler optimizes case statements. Their suggestions to solve the problem are the following:

    1) Use Xcode 3.2.1 with 11.1 compiler.

    2) Use 11.1 compiler with the option -use-asm with Xcode 3.2.2, 3.2.3, 3.2.4. It should fix most cases but there are some cases when even generating object file through external assembler L* symbols still may appear in object file. Those cases are usually constant string literals placed in cstring section

    3) Use Intel Composer XE.

    My Xcode is version 3.2.6, but solution 2) solved my problem. I remain however quite puzzled about this (and the lack of documentation on the web).

    Thanks.