缓冲区大小:N*sizeof(type) 还是 sizeof(var)? C++

发布于 2024-09-03 06:03:47 字数 419 浏览 14 评论 0原文

我刚刚开始使用 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 技术交流群。

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

发布评论

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

评论(4

━╋う一瞬間旳綻放 2024-09-10 06:03:47

两者都不正确。

您正在使用 StringCchPrintf(),它对字符计数而不是字节进行操作。 sizeof(buffer) 返回缓冲区的大小(以字节为单位),与 255*sizeof(char) 一样。 255*sizeof(char) 也有一个缺点,即您在两个地方复制了数组的大小 - 如果您更改缓冲区的大小但在调用 StringCchPrintf 时忘记了,则会出现错误。

这恰好有效,因为 sizeof(char) 始终为 1。

您还将 buffer 指定为 char,但在字符串周围使用 TEXT() - 使用 UNICODE 编译将导致中断。

以下任何一项都是正确的:

char buffer[255];
StringCchPrintf(buffer, ARRAYSIZE(buffer), "%s", X);

TCHAR buffer[255];
StringCchPrintf(buffer, ARRAYSIZE(buffer), TEXT("%s"), X);

char buffer[255];
StringCbPrintf(buffer, sizeof(buffer), "%s", X);

TCHAR buffer[255];
StringCbPrintf(buffer, sizeof(buffer), TEXT("%s"), X);

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:

char buffer[255];
StringCchPrintf(buffer, ARRAYSIZE(buffer), "%s", X);

TCHAR buffer[255];
StringCchPrintf(buffer, ARRAYSIZE(buffer), TEXT("%s"), X);

char buffer[255];
StringCbPrintf(buffer, sizeof(buffer), "%s", X);

TCHAR buffer[255];
StringCbPrintf(buffer, sizeof(buffer), TEXT("%s"), X);
揽清风入怀 2024-09-10 06:03:47

考虑到上述两个变体,第一个变体要好得多,因为它不会重复“神奇常数”255。如果您希望第二个变体与第一个变体竞争,您必须这样做

const size_t BUFFER_SIZE = 255;
char buffer[BUFFER_SIZE];
StringCchPrintf(buf, BUFFER_SIZE*sizeof(char), TEXT("%s"), X);

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

const size_t BUFFER_SIZE = 255;
char buffer[BUFFER_SIZE];
StringCchPrintf(buf, BUFFER_SIZE*sizeof(char), TEXT("%s"), X);
紫轩蝶泪 2024-09-10 06:03:47

sizeof(buffer) 适用于静态分配的数组,但不适用于动态分配的数组:

char buffer[255];
cout << sizeof(buffer) << endl;   // prints 255
char *p = new char[255];
cout << sizeof(p) << endl;        // prints 8 (on a 64-bit machine)
delete[] p;
return 0;

考虑到这一点,我建议始终使用 N * sizeof(type) 以保持一致性并避免微妙的影响错误。

sizeof(buffer) will work for a statically allocated array but not for a dynamically allocated array:

char buffer[255];
cout << sizeof(buffer) << endl;   // prints 255
char *p = new char[255];
cout << sizeof(p) << endl;        // prints 8 (on a 64-bit machine)
delete[] p;
return 0;

With this in mind I would recommend always using N * sizeof(type) for the sake of consistency and to avoid subtle bugs.

南街九尾狐 2024-09-10 06:03:47

您应该使用常量来表示大小,而不是像您那样使用整数。

根据 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?

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