缓冲区大小:N*sizeof(type) 还是 sizeof(var)? C++
我刚刚开始使用 cpp,我一直在遵循不同的示例来向它们学习,我发现缓冲区大小以不同的方式设置,例如:
char buffer[255];
StringCchPrintf(buffer, sizeof(buffer), TEXT("%s"), X);
VS
char buffer[255];
StringCchPrintf(buffer, 255*sizeof(char), TEXT("%s"), X);
< strong>哪一种是正确的使用方法?
我在 InternetReadFile、ZeroMemory 和 MultiByteToWideChar 等其他函数中看到过这一点。
I am just starting with cpp and I've been following different examples to learn from them, and I see that buffer size is set in different ways, for example:
char buffer[255];
StringCchPrintf(buffer, sizeof(buffer), TEXT("%s"), X);
VS
char buffer[255];
StringCchPrintf(buffer, 255*sizeof(char), TEXT("%s"), X);
Which one is the correct way to use it?
I've seen this in other functions like InternetReadFile, ZeroMemory and MultiByteToWideChar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
两者都不正确。
您正在使用 StringCchPrintf(),它对字符计数而不是字节进行操作。 sizeof(buffer) 返回缓冲区的大小(以字节为单位),与 255*sizeof(char) 一样。 255*sizeof(char) 也有一个缺点,即您在两个地方复制了数组的大小 - 如果您更改缓冲区的大小但在调用 StringCchPrintf 时忘记了,则会出现错误。
这恰好有效,因为 sizeof(char) 始终为 1。
您还将 buffer 指定为 char,但在字符串周围使用 TEXT() - 使用 UNICODE 编译将导致中断。
以下任何一项都是正确的:
Neither is correct.
You are using StringCchPrintf(), which operates on the count of characters, not bytes. sizeof(buffer) returns the size of buffer in bytes, as does 255*sizeof(char). 255*sizeof(char) also has the disadvantage that you are duplicating the size of the array in two places - if you change the size of buffer but forget in the call to StringCchPrintf, you have a bug.
This happens to work since sizeof(char) is always 1.
You are also specifying buffer as char, but use TEXT() around the string - compiling with UNICODE will cause a break.
Any of the following would be correct:
考虑到上述两个变体,第一个变体要好得多,因为它不会重复“神奇常数”255。如果您希望第二个变体与第一个变体竞争,您必须这样做
Given the above two variants, the first one is vastly better, since it does not repeat the "magic constant" 255. If you wanted the second variant to be competitive with first, you have to do it as
sizeof(buffer)
适用于静态分配的数组,但不适用于动态分配的数组:考虑到这一点,我建议始终使用 N * sizeof(type) 以保持一致性并避免微妙的影响错误。
sizeof(buffer)
will work for a statically allocated array but not for a dynamically allocated array:With this in mind I would recommend always using N * sizeof(type) for the sake of consistency and to avoid subtle bugs.
您应该使用常量来表示大小,而不是像您那样使用整数。
根据 Microsoft,计算所需内容的正确形式是:
sizeof array / sizeof array[0]
http://msdn.microsoft.com/en-us/library/4s7x1k91%28VS.71%29.aspx
此外, sizeof 并不完美,因为在某些情况下它将返回指针的大小,而不是数组的大小。在这种情况下,术语 SIZE OF 有点误导,因为您必须问自己 - 我实际上得到的 SIZE OF 是什么?
You should use constants for the size, not integers like you did.
Per Microsoft, the correct form to calculate what you want is this:
sizeof array / sizeof array[0]
http://msdn.microsoft.com/en-us/library/4s7x1k91%28VS.71%29.aspx
Also, sizeof isn't perfect because in some instances it will return the size of the pointer and not the size of the array. The term SIZE OF is a little misleading in this case, because you have to ask yourself - what am I actually getting the SIZE OF?