「GCC いろいろ」の編集履歴(バックアップ)一覧はこちら
「GCC いろいろ」(2010/10/01 (金) 12:10:26) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
GNU Compiler Collection に関する小ネタ、気づき、メモなど。
#contents()
----
*Type-punned pointerについての警告が出たらどうするか
"警告: dereferencing type-punned pointer will break strict-aliasing rules"が出たときにどうするか。
詳細は[[http://www.up-cat.net/gcc%2Boption.html]]を見てもらうとして、解決法としては
- unionを使う
- &html(__attribute__((may_alias))) を使う
- -fno-strict-aliasing オプションを付ける
が挙げられる。移植性を考えるとunionを使うのが無難かも。
なお、[[http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Optimize-Options.html]]によると
A character type may alias any other type.
とのことらしい。そこで、例えばaが32ビット整数型へのポインタだとして
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 ソースを見てみると
#highlight(c){{
/* Convert a string to an int. */
int
atoi (const char *nptr)
{
return (int) strtol (nptr, (char **) NULL, 10);
} }}
と、単なるstdtolのラッパーになっている。 atof, atoll も同様に strtod, strtoll のラッパーである。
----
GNU Compiler Collection の Cコンパイラに関する小ネタ、気づき、メモなど。
#contents()
----
*Type-punned pointerについての警告が出たらどうするか
"警告: dereferencing type-punned pointer will break strict-aliasing rules"が出たときにどうするか。
詳細は[[http://www.up-cat.net/gcc%2Boption.html]]を見てもらうとして、解決法としては
- unionを使う
- &html(__attribute__((may_alias))) を使う
- -fno-strict-aliasing オプションを付ける
が挙げられる。移植性を考えるとunionを使うのが無難かも。
なお、[[http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Optimize-Options.html]]によると
A character type may alias any other type.
とのことらしい。そこで、例えばaが32ビット整数型へのポインタだとして
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 ソースを見てみると
#highlight(c){{
/* Convert a string to an int. */
int
atoi (const char *nptr)
{
return (int) strtol (nptr, (char **) NULL, 10);
} }}
と、単なるstdtolのラッパーになっている。 atof, atoll も同様に strtod, strtoll のラッパーである。
----