Jul. 28th, 2015

izard: (Default)
I just wrote the following code:
// very simplified:
int *init_impl(int *array, int size)
{
  printf ("n = %i\n", n);
  // initialize array

  // return pointer to last element
  return &array[size -1];
}

void init(int *array, int size)
{
    if (size < 100) return init_impl(array, size);
// if size >= 100, initialize array as chunks of size 100
}

int main(int argc, char *argv[])
{
 // malloc array
  init (array, atoi(argv[1]));
  process_array();  
}


This works when compiling as -O0, but with -O3 and size < 100 init_impl call is optimized out - printf is not called and array is not initalized.

The fix:
- if (size < 100) return init_impl(array, size);
+ if (size < 100) { init_impl(array, size); return; };

Is it a compiler bug or a C feature?

Profile

izard: (Default)
izard

August 2025

S M T W T F S
     12
3456789
10111213 141516
17181920212223
24252627282930
31      

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 15th, 2025 08:09 am
Powered by Dreamwidth Studios