Best search algorithm to find condition within a range

Marko Rauhamaa marko at pacujo.net
Thu Apr 9 09:49:46 EDT 2015


Marko Rauhamaa <marko at pacujo.net>:

> In this day and age, heavily optimized compilers (including gcc)
> actively exploit undefined behavior in code generation.

Example: Find the value of INT_MAX programmatically.

========================================================================
#include <stdio.h>

int main()
{
    int n = 1;
    while (n + 1 > n)
        n++;
    printf("%d\n", n);
    return 0;
}
========================================================================

========================================================================
$ # Use gcc-4.9.2
$ cc -o test test.c
$ ./test
2147483647
$ cc -O9 -o test test.c
$ ./test
^C
========================================================================

The command "cc -S -O9 test.c" shows that the compiler compiles the
while loop into:

========================================================================
.L2:
        jmp     .L2
========================================================================


Marko



More information about the Python-list mailing list