源代码:
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
之前一直在计数。用shortL"abc"
生成的wchar_t
不会用常规的wchar_t
终止,因此wcslen
将继续在随机内存中运行,直到找到一个。