Sizeof char 数组给出的值极不符合标准

发布于 2024-11-04 06:02:15 字数 3795 浏览 0 评论 0原文

请考虑以下事项。为什么 sizeof 返回“23”?我怎样才能得到真实的尺寸? (不计算嘿)

char defaultsettings[] = "<?xml version=\"1.0\"?>\n";
strcat(defaultsettings, "<CsSettings>\n<options>\n");
strcat(defaultsettings, "<spin name=\"maxTries\" value=\"1000\" />\n");
strcat(defaultsettings, "<spin name=\"rollCount\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"matchItems\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"highlightItems\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"buyListMode\" value=\"0\" />\n");
strcat(defaultsettings, "<radio name=\"matchItemType\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"matchLocations\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightLocations\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"allMissionsSameLocation\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightFind\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightReturn\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightRepair\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightPerson\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightKill\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignoreFind\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignoreReturn\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignoreRepair\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignorePerson\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignoreKill\" value=\"1\" />\n");
strcat(defaultsettings, "<combo name=\"itemValueBase\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"matchMissionValue\" value=\"0\" />\n");
strcat(defaultsettings, "<spin name=\"matchMissionMinValue\" value=\"10000\" />\n");
strcat(defaultsettings, "<check name=\"levelCalcFixerMode\" value=\"0\" />\n");
strcat(defaultsettings, "<spin name=\"bneBonus\" value=\"1\" />\n");
strcat(defaultsettings, "<spin name=\"levelCalc\" value=\"1\" />\n");
strcat(defaultsettings, "<spin name=\"missionCalc\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"log\" value=\"0\" />\n");
strcat(defaultsettings, "<entry name=\"useKey\" value=\"e\" />\n");
strcat(defaultsettings, "</options>\n<items>\n</items>\n");
strcat(defaultsettings, "<locations>\n<location>\n<string>Milky way</string>\n");
strcat(defaultsettings, "<x>\n<var>3050</var>\n<var>3400</var>\n</x>\n");
strcat(defaultsettings, "<y>\n<var>1000</var>\n<var>1200</var>\n</y>\n");
strcat(defaultsettings, "<ignore>1</ignore>\n<enabled>1</enabled>\n");
strcat(defaultsettings, "<coordsEnabled>1</coordsEnabled>\n");
strcat(defaultsettings, "</location>\n<location>\n<string>Milky way</string>\n");
strcat(defaultsettings, "<x>\n<var>3500</var>\n<var>3850</var>\n</x>\n");
strcat(defaultsettings, "<y>\n<var>700</var>\n<var>950</var>\n</y>\n");
strcat(defaultsettings, "<ignore>1</ignore>\n<enabled>1</enabled>\n");
strcat(defaultsettings, "<coordsEnabled>1</coordsEnabled>\n");
strcat(defaultsettings, "</location>\n</locations>\n</CsSettings>");
printf("%s\nSize: %d\n\n",defaultsettings,sizeof(defaultsettings));

输出:

[...]
</location>
</locations>
</CsSettings>
Size: 23

Consider the following. Why does sizeof return "23"? And how can I get the real size? (without counting heh)

char defaultsettings[] = "<?xml version=\"1.0\"?>\n";
strcat(defaultsettings, "<CsSettings>\n<options>\n");
strcat(defaultsettings, "<spin name=\"maxTries\" value=\"1000\" />\n");
strcat(defaultsettings, "<spin name=\"rollCount\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"matchItems\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"highlightItems\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"buyListMode\" value=\"0\" />\n");
strcat(defaultsettings, "<radio name=\"matchItemType\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"matchLocations\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightLocations\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"allMissionsSameLocation\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightFind\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightReturn\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightRepair\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightPerson\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"highlightKill\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignoreFind\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignoreReturn\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignoreRepair\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignorePerson\" value=\"0\" />\n");
strcat(defaultsettings, "<check name=\"ignoreKill\" value=\"1\" />\n");
strcat(defaultsettings, "<combo name=\"itemValueBase\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"matchMissionValue\" value=\"0\" />\n");
strcat(defaultsettings, "<spin name=\"matchMissionMinValue\" value=\"10000\" />\n");
strcat(defaultsettings, "<check name=\"levelCalcFixerMode\" value=\"0\" />\n");
strcat(defaultsettings, "<spin name=\"bneBonus\" value=\"1\" />\n");
strcat(defaultsettings, "<spin name=\"levelCalc\" value=\"1\" />\n");
strcat(defaultsettings, "<spin name=\"missionCalc\" value=\"1\" />\n");
strcat(defaultsettings, "<check name=\"log\" value=\"0\" />\n");
strcat(defaultsettings, "<entry name=\"useKey\" value=\"e\" />\n");
strcat(defaultsettings, "</options>\n<items>\n</items>\n");
strcat(defaultsettings, "<locations>\n<location>\n<string>Milky way</string>\n");
strcat(defaultsettings, "<x>\n<var>3050</var>\n<var>3400</var>\n</x>\n");
strcat(defaultsettings, "<y>\n<var>1000</var>\n<var>1200</var>\n</y>\n");
strcat(defaultsettings, "<ignore>1</ignore>\n<enabled>1</enabled>\n");
strcat(defaultsettings, "<coordsEnabled>1</coordsEnabled>\n");
strcat(defaultsettings, "</location>\n<location>\n<string>Milky way</string>\n");
strcat(defaultsettings, "<x>\n<var>3500</var>\n<var>3850</var>\n</x>\n");
strcat(defaultsettings, "<y>\n<var>700</var>\n<var>950</var>\n</y>\n");
strcat(defaultsettings, "<ignore>1</ignore>\n<enabled>1</enabled>\n");
strcat(defaultsettings, "<coordsEnabled>1</coordsEnabled>\n");
strcat(defaultsettings, "</location>\n</locations>\n</CsSettings>");
printf("%s\nSize: %d\n\n",defaultsettings,sizeof(defaultsettings));

Output:

[...]
</location>
</locations>
</CsSettings>
Size: 23

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残月升风 2024-11-11 06:02:15

哦亲爱的!

因为您只为第一行的 23 个字符分配了内存,然后为其余数据覆盖未分配的内存。

这里实际上不需要任何 strcat,您只需使用 C 预处理器的一个功能来连接相邻的字符串文字:

const char long_string[]  = "First line"
"next line"
"another line"
"even more text";

这将为您提供一个适当大小的数组。

Oh dear!

Because you have only allocated memory for 23 chars on the first line, and are then overwriting unallocated memory for the rest of the data.

You don't actually need any of the strcat's here, you can just use a feature of then C preprocessor that concatenates adjacent string literals:

const char long_string[]  = "First line"
"next line"
"another line"
"even more text";

This will get you one array of the proper size.

谁的年少不轻狂 2024-11-11 06:02:15

因为数组的大小是由一条语句定义的:

char defaultsettings[] = "<?xml version=\"1.0\"?>\n";

其余的 strcat() 调用只是写入超出数组末尾的内容(这是未定义的行为)。

我有点惊讶你的测试没有崩溃。

Because the size of the array is defined by the one statement:

char defaultsettings[] = "<?xml version=\"1.0\"?>\n";

The remaining strcat() calls are just writing past the end of the array (which is undefined behavior).

I'm a little surprised your test doesn't crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文