Sizeof char 数组给出的值极不符合标准
请考虑以下事项。为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦亲爱的!
因为您只为第一行的 23 个字符分配了内存,然后为其余数据覆盖未分配的内存。
这里实际上不需要任何 strcat,您只需使用 C 预处理器的一个功能来连接相邻的字符串文字:
这将为您提供一个适当大小的数组。
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:
This will get you one array of the proper size.
因为数组的大小是由一条语句定义的:
其余的
strcat()
调用只是写入超出数组末尾的内容(这是未定义的行为)。我有点惊讶你的测试没有崩溃。
Because the size of the array is defined by the one statement:
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.