ソースコード:
const wchar_t* x = L"abc";
printf("%d\n",wcslen(x));
これを でコンパイルした結果g++ -fshort-wchar xxx.cpp -o xxx
が得られます。15
なんで?
gcc のドキュメントでは次のように警告されています。
*Warning:* the `-fshort-wchar' switch causes GCC to generate code
that is not binary compatible with code generated without that
switch. Use it to conform to a non-default application binary
interface.
おそらく、wcslen
リンク先の は通常の長さwchar_t
の s で生成されたものであり、その結果、 が見つかるまでカウントされます(regular_wchar_t)0
。L"abc"
short で生成されたwchar_t
は通常の で終了しないwchar_t
ため、wcslen
ランダム メモリが見つかるまで実行され続けます。