GNU Compiler Collection の Cコンパイラに関する小ネタ、気づき、メモなど。
Type-punned pointerについての警告が出たらどうするか
- unionを使う
- __attribute__((may_alias)) を使う
- -fno-strict-aliasing オプションを付ける
が挙げられる。移植性を考えるとunionを使うのが無難かも。
float *b = *(float *) a;
という警告のでるコードがあったとする。これを
char *c = (char *) a;
float *b = *(float *) c;
とすると警告が消える。ただ、同じように見えても
float *b = *(float *)(char *) c;
だとダメ。
警告は消えたけど、これで動作が保障されるかどうかは分からない。
結論:union使え。
atoi は stdtol より, atof は stdtod より, atoll は strtoll より速い、ということはない。
GCCというよりも、glibcネタ。
atoi は stdtol よりもシンプル(エラー処理なし、10進数限定)なので、前者が後者よりも速いであろうと思いたくなるが、glibc-2.9の atoi ソースを見てみると
/* Convert a string to an int. */
int
atoi (const char *nptr)
{
return (int) strtol (nptr, (char **) NULL, 10);
}
と、単なるstdtolのラッパーになっている。 atof, atoll も同様に strtod, strtoll のラッパーである。
最終更新:2010年10月01日 12:10