よくあるこういうのにハマった。

ついやってしまうので自戒を込めて。

(C99)

#include <stdio.h>

struct vector {
    double elts[3];
};

#define create_vector_1(a, b, c) (struct vector){ a, b, c }

static inline struct vector create_vector_2(double a, double b, double c)
{
    return (struct vector){ a, b, c };
}

static void print_vector(struct vector v)
{
    printf("(%g, %g, %g)\n", v.elts[0], v.elts[1], v.elts[2]);
}

int main()
{
    {
        double f = 0;
        print_vector(create_vector_1(++f, ++f, ++f));
    }
    {
        volatile double f = 0;
        print_vector(create_vector_1(++f, ++f, ++f));
    }
    {
        double f = 0;
        print_vector(create_vector_2(++f, ++f, ++f));
    }
    {
        volatile double f = 0;
        print_vector(create_vector_2(++f, ++f, ++f));
    }
}

コンパイラ:

gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9) 

実行結果 (-O0):

(3, 3, 3)
(1, 2, 3)
(3, 2, 1)
(3, 2, 1)

実行結果 (-O2):

(3, 3, 3)
(1, 2, 3)
(3, 3, 3)
(3, 2, 1)