简体   繁体   English

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

[英]Buffer size: N*sizeof(type) or sizeof(var)? C++

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: 我刚刚开始使用cpp,我一直在关注不同的示例以向他们学习,我看到缓冲区大小以不同的方式设置,例如:

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

VS 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. 我在其他函数中看过这个,比如InternetReadFile,ZeroMemory和MultiByteToWideChar。

Neither is correct. 两者都不正确。

You are using StringCchPrintf(), which operates on the count of characters, not bytes. 您正在使用StringCchPrintf(),它根据字符数而非字节进行操作。 sizeof(buffer) returns the size of buffer in bytes, as does 255*sizeof(char). sizeof(buffer)返回缓冲区的大小(以字节为单位),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. 255 * sizeof(char)的缺点是你在两个地方复制数组的大小 - 如果你改变缓冲区的大小但忘记调用StringCchPrintf,你就有一个bug。

This happens to work since sizeof(char) is always 1. 这恰好起作用,因为sizeof(char)始终为1。

You are also specifying buffer as char, but use TEXT() around the string - compiling with UNICODE will cause a break. 您还将缓冲区指定为char,但在字符串周围使用TEXT() - 使用UNICODE进行编译将导致中断。

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);

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 鉴于以上两种变体,第一种变体要好得多,因为它不会重复“魔术常数”255.如果你想让第二种变体与第一变体竞争,你必须这样做

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

sizeof(buffer) will work for a statically allocated array but not for a dynamically allocated array: 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;

With this in mind I would recommend always using N * sizeof(type) for the sake of consistency and to avoid subtle bugs. 考虑到这一点,我建议始终使用N * sizeof(类型)以保持一致性并避免细微的错误。

You should use constants for the size, not integers like you did. 你应该使用常量来表示大小,而不是像你那样使用整数。

Per Microsoft, the correct form to calculate what you want is this: 根据Microsoft,计算所需内容的正确形式是:

sizeof array / sizeof array[0] sizeof array / sizeof数组[0]

http://msdn.microsoft.com/en-us/library/4s7x1k91%28VS.71%29.aspx 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. 此外,sizeof并不完美,因为在某些情况下,它将返回指针的大小而不是数组的大小。 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 ? 在这种情况下,SIZE OF这个词有点误导,因为你必须问自己 - 我实际上得到的是SIZE OF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM