初始化无符号字符的正确方法*

发布于 2024-10-15 16:50:12 字数 193 浏览 7 评论 0原文

初始化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 技术交流群。

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

发布评论

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

评论(7

帥小哥 2024-10-22 16:50:12

要“正确”初始化指针(unsigned char *,如您的示例中所示),您只需要执行一个简单的操作。

unsigned char *tempBuffer = NULL;

如果您想初始化 unsigned char 数组,您可以执行以下任一操作:

unsigned char *tempBuffer = new unsigned char[1024]();
// and do not forget to delete it later
delete[] tempBuffer;

或者

unsigned char tempBuffer[1024] = {};

我还建议您查看 std::vector,您可以像这样初始化:

std::vector<unsigned char> tempBuffer(1024, 0);

To "properly" initialize a pointer (unsigned char * as in your example), you need to do just a simple

unsigned char *tempBuffer = NULL;

If you want to initialize an array of unsigned chars, you can do either of following things:

unsigned char *tempBuffer = new unsigned char[1024]();
// and do not forget to delete it later
delete[] tempBuffer;

or

unsigned char tempBuffer[1024] = {};

I would also recommend to take a look at std::vector<unsigned char>, which you can initialize like this:

std::vector<unsigned char> tempBuffer(1024, 0);
不交电费瞎发啥光 2024-10-22 16:50:12

第二种方法会给你留下一个空指针。请注意,您没有在这里为缓冲区声明任何空间,而是声明了一个指向必须在其他地方创建的缓冲区的指针。如果将其初始化为 "",则指针将指向一个只有一个字节(空终止符)的静态缓冲区。如果您想要一个可以稍后写入字符的缓冲区,请使用 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 like malloc.

思念满溢 2024-10-22 16:50:12

由于它是一个指针,您要么想首先将其初始化为 NULL,如下所示:

unsigned char* tempBuffer = NULL;
unsigned char* tempBuffer = 0;

要么分配变量的地址,如下所示:

unsigned char c = 'c';

unsigned char* tempBuffer = &c;

编辑:
如果您想分配一个字符串,可以按如下方式完成:

unsigned char myString [] = "This is my string";
unsigned char* tmpBuffer = &myString[0];

As it's a pointer, you either want to initialize it to NULL first like this:

unsigned char* tempBuffer = NULL;
unsigned char* tempBuffer = 0;

or assign an address of a variable, like so:

unsigned char c = 'c';

unsigned char* tempBuffer = &c;

EDIT:
If you wish to assign a string, this can be done as follows:

unsigned char myString [] = "This is my string";
unsigned char* tmpBuffer = &myString[0];
好倦 2024-10-22 16:50:12

如果您在编译时知道缓冲区的大小:

unsigned char buffer[SIZE] = {0};

对于动态分配的缓冲区(在运行时或在上分配的缓冲区):

1.首选 new 运算符:

unsigned char * buffer = 0;  // Pointer to a buffer, buffer not allocated.
buffer = new unsigned char [runtime_size];

2.“初始化”或填充简单值的解决方案有很多:

std::fill(buffer, buffer + runtime_size, 0); // Prefer to use STL
memset(buffer, 0, runtime_size);
for (i = 0; i < runtime_size; ++i) *buffer++ = 0;  // Using a loop

3.C语言端提供一次调用即可分配和初始化。
但是,该函数不会调用对象的构造函数:

buffer = calloc(runtime_size, sizeof(unsigned char))

请注意,这也会将缓冲区中的所有位设置为零;您无法选择初始值。

If you know the size of the buffer at compile time:

unsigned char buffer[SIZE] = {0};

For dynamically allocated buffers (buffers allocated during run-time or on the heap):

1.Prefer the new operator:

unsigned char * buffer = 0;  // Pointer to a buffer, buffer not allocated.
buffer = new unsigned char [runtime_size];

2.Many solutions to "initialize" or fill with a simple value:

std::fill(buffer, buffer + runtime_size, 0); // Prefer to use STL
memset(buffer, 0, runtime_size);
for (i = 0; i < runtime_size; ++i) *buffer++ = 0;  // Using a loop

3.The C language side provides allocation and initialization with one call.
However, the function does not call the object's constructors:

buffer = calloc(runtime_size, sizeof(unsigned char))

Note that this also sets all bits in the buffer to zero; you don't get a choice in the initial value.

久而酒知 2024-10-22 16:50:12

这取决于您想要实现的目标(例如您是否想要修改字符串)。有关更多信息,请参见 http://c-faq.com/charstring/index.html细节。

请注意,如果您声明一个指向字符串文字的指针,则它应该是 const,即:

const unsigned char *tempBuffer = "";

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.:

const unsigned char *tempBuffer = "";
×眷恋的温暖 2024-10-22 16:50:12

如果计划将其作为缓冲区,并且您希望稍后将其移动以指向某个内容,则将其初始化为 NULL,直到它真正指向您要写入的某个位置,而不是空字符串。

unsigned char * tempBuffer = NULL;
std::vector< unsigned char > realBuffer( 1024 );
tempBuffer = &realBuffer[0]; // now it really points to writable memory
memcpy( tempBuffer, someStuff, someSizeThatFits );

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 * tempBuffer = NULL;
std::vector< unsigned char > realBuffer( 1024 );
tempBuffer = &realBuffer[0]; // now it really points to writable memory
memcpy( tempBuffer, someStuff, someSizeThatFits );
花开浅夏 2024-10-22 16:50:12

答案取决于您打算使用 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.

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