初始化无符号字符的正确方法*
初始化unsigned char*
的正确方法是什么?我目前正在这样做:
unsigned char* tempBuffer;
tempBuffer = "";
或者我应该使用 memset(tempBuffer, 0, sizeof(tempBuffer)); ?
What is the proper way to initialize unsigned char*
? I am currently doing this:
unsigned char* tempBuffer;
tempBuffer = "";
Or should I be using memset(tempBuffer, 0, sizeof(tempBuffer));
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要“正确”初始化指针(
unsigned char *
,如您的示例中所示),您只需要执行一个简单的操作。如果您想初始化
unsigned char
数组,您可以执行以下任一操作:或者
我还建议您查看
std::vector
,您可以像这样初始化:To "properly" initialize a pointer (
unsigned char *
as in your example), you need to do just a simpleIf you want to initialize an array of
unsigned char
s, you can do either of following things:or
I would also recommend to take a look at
std::vector<unsigned char>
, which you can initialize like this:第二种方法会给你留下一个空指针。请注意,您没有在这里为缓冲区声明任何空间,而是声明了一个指向必须在其他地方创建的缓冲区的指针。如果将其初始化为
""
,则指针将指向一个只有一个字节(空终止符)的静态缓冲区。如果您想要一个可以稍后写入字符的缓冲区,请使用 Fred 的数组建议或类似malloc
的内容。The second method will leave you with a null pointer. Note that you aren't declaring any space for a buffer here, you're declaring a pointer to a buffer that must be created elsewhere. If you initialize it to
""
, that will make the pointer point to a static buffer with exactly one byte—the null terminator. If you want a buffer you can write characters into later, use Fred's array suggestion or something likemalloc
.由于它是一个指针,您要么想首先将其初始化为 NULL,如下所示:
要么分配变量的地址,如下所示:
编辑:
如果您想分配一个字符串,可以按如下方式完成:
As it's a pointer, you either want to initialize it to NULL first like this:
or assign an address of a variable, like so:
EDIT:
If you wish to assign a string, this can be done as follows:
如果您在编译时知道缓冲区的大小:
对于动态分配的缓冲区(在运行时或在堆上分配的缓冲区):
1.首选
new
运算符:2.“初始化”或填充简单值的解决方案有很多:
3.C语言端提供一次调用即可分配和初始化。
但是,该函数不会调用对象的构造函数:
请注意,这也会将缓冲区中的所有位设置为零;您无法选择初始值。
If you know the size of the buffer at compile time:
For dynamically allocated buffers (buffers allocated during run-time or on the heap):
1.Prefer the
new
operator:2.Many solutions to "initialize" or fill with a simple value:
3.The C language side provides allocation and initialization with one call.
However, the function does not call the object's constructors:
Note that this also sets all bits in the buffer to zero; you don't get a choice in the initial value.
这取决于您想要实现的目标(例如您是否想要修改字符串)。有关更多信息,请参见 http://c-faq.com/charstring/index.html细节。
请注意,如果您声明一个指向字符串文字的指针,则它应该是 const,即:
It depends on what you want to achieve (e.g. do you ever want to modify the string). See e.g. http://c-faq.com/charstring/index.html for more details.
Note that if you declare a pointer to a string literal, it should be
const
, i.e.:如果计划将其作为缓冲区,并且您希望稍后将其移动以指向某个内容,则将其初始化为 NULL,直到它真正指向您要写入的某个位置,而不是空字符串。
If the plan is for it to be a buffer and you want to move it later to point to something, then initialise it to NULL until it really points somewhere to which you want to write, not an empty string.
答案取决于您打算使用 unsigned char 来做什么。 char 只不过是一个小整数,在 99% 的实现中都是 8 位。
C 恰好有一些与 char 非常适合的字符串支持,但这并不限制 char 对字符串的使用。
初始化指针的正确方法取决于 1) 其范围和 2) 其预期用途。
如果指针被声明为静态和/或在文件范围内声明,则 ISO C/C++ 保证将其初始化为 NULL。编程风格纯粹主义者仍然会将其设置为 NULL 以保持其风格与局部作用域变量一致,但理论上这样做是没有意义的。
至于初始化为什么...设置为NULL。不要将其设置为指向“”,因为这将分配一个包含空终止的静态虚拟字节,一旦将指针分配给其他内容,这将成为一个微小的静态内存泄漏。
有人可能会质疑为什么您首先需要将其初始化为任何内容。只需在使用之前将其设置为有效的值即可。如果您担心在给指针提供有效值之前使用指针,则应该使用适当的静态分析器来查找此类简单的错误。即使大多数编译器也会捕获该错误并向您发出警告。
The answer depends on what you inted to use the unsigned char for. A char is nothing else but a small integer, which is of size 8 bits on 99% of all implementations.
C happens to have some string support that fits well with char, but that doesn't limit the usage of char to strings.
The proper way to initialize a pointer depends on 1) its scope and 2) its intended use.
If the pointer is declared static, and/or declared at file scope, then ISO C/C++ guarantees that it is initialized to NULL. Programming style purists would still set it to NULL to keep their style consistent with local scope variables, but theoretically it is pointless to do so.
As for what to initialize it to... set it to NULL. Don't set it to point at "", because that will allocate a static dummy byte containing a null termination, which will become a tiny little static memory leak as soon as the pointer is assigned to something else.
One may question why you need to initialize it to anything at all in the first place. Just set it to something valid before using it. If you worry about using a pointer before giving it a valid value, you should get a proper static analyzer to find such simple bugs. Even most compilers will catch that bug and give you a warning.