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

September 2025

S M T W T F S
 1 23456
78910111213
14151617181920
21222324252627
282930    

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 11th, 2025 09:27 pm
Powered by Dreamwidth Studios