简体   繁体   English

如何在缓冲区末尾添加0x00

[英]How to add 0x00 at the end of a buffer

I have a piece of code as: 我有一段代码:

char* buffer = new char[12];
memcpy(newbuffer, bytes, 10); //bytes is const void *

Now I want the last 2 bytes which I have not used should be appended with 0000 . 现在,我希望将未使用的最后2个字节附加0000

How can I achieve that? 我该如何实现?

Easy as this: 如此简单:

buffer[10]=buffer[11]=0;

If it was a longer range you could have used memset or std::fill . 如果范围较长,则可以使用memsetstd::fill

使用内存集。

memset(buffer + 10, 0, 2);

Initialize the whole buffer before calling memcpy and its done for you: 在调用memcpy之前初始化整个缓冲区,并为您完成:

char* buffer = new char[12];
memset(buffer, 0, 12);
memcpy(buffer, bytes, 10);

You can just assign them directly: 您可以直接分配它们:

buffer[10] = 0;
buffer[11] = 0;

Or put it in a loop if you prefer. 或者,如果您愿意,可以将其循环。

只需将它们设置为0。

buffer[10] = buffer[11] = 0;

errr.. errr ..

buffer[10] = 0;
buffer[11] = 0;

If you need to do that for more than only two elements: 如果您需要为两个以上的元素执行此操作:

memset( & buffer[10], 0, 2 );

More generic: 更通用:

// sets the next n elements of your array of type T
// pointed by pointer to 0.
memset( pointer, 0, n*sizeof(T) );

For just two elements the simple solution already suggested by many will probably be clearer and faster. 对于两个要素,许多人已经建议的简单解决方案可能会更清晰,更快捷。

Since you are using C++, you should eschew manual memory management and use the Standard Library facilities that are available to you, like std::vector . 由于您使用的是C ++,因此应该避免进行手动内存管理,并使用标准库功能,例如std::vector It is near impossible to correctly manage memory manually in C++ and it is completely necessary in all but a select few cases (like in low-level library code). 在C ++中几乎不可能正确地手动管理内存,并且在除少数几种情况下(例如在低级库代码中)几乎完全必要。

Given: 鉴于:

const char* bytes; 
std::size_t n_bytes = 10; 

Any of the following will work: 以下任何一项均可使用:

std::vector<char> buffer(bytes, bytes + n_bytes);
buffer.push_back(0); // null-terminate the buffer

or: 要么:

std::vector<char> buffer(n_bytes + 1);
std::copy(bytes, bytes + n_bytes, buffer.begin());

or: 要么:

std::vector<char> buffer;
buffer.reserve(n_bytes + 1);
std::copy(bytes, bytes + n_bytes, std::back_inserter(buffer));
buffer.push_back(0);

Which performs best depends on how std::vector is implemented by your compiler. 哪个执行效果最好取决于编译器如何实现std::vector In practice, unless you are creating and destroying buffers at a relatively high frequency, you won't notice a sizable performanace difference. 实际上,除非您以相对较高的频率创建和销毁缓冲区,否则您不会注意到可观的性能差异。 The first of the three solutions presented here is the cleanest and easiest to understand at first glance. 乍看之下,这里介绍的三个解决方案中的第一个是最干净,最容易理解的。

By using std::vector , you don't need to worry about freeing the memory yourself and you don't need to worry about correctly destroying the dynamically allocated object when an exception causes a function to return early. 通过使用std::vector ,您不必担心自己释放内存,也不必担心在异常导致函数提早返回时正确销毁动态分配的对象。 In addition, a good implementation of the Standard Library containers (like the one provided with Visual C++) provides runtime checks in debug builds that help you to find common bugs like off-by-one errors or iterator invalidation errors. 此外,标准库容器的良好实现(例如Visual C ++所提供的容器)可以在调试版本中提供运行时检查,从而帮助您查找常见的错误,例如一次性错误或迭代器无效错误。

另一种方法是在写入缓冲区之前,先使用memsetstd::fillstd::fill

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

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