源代码:

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

10-08 11:57