简体   繁体   English

什么是“ std :: vector” <unsigned char> 数据=示例”?

[英]What is “std::vector<unsigned char> Data = example”?

std::vector<unsigned char> Data = example

I found this on the internet somewhere and I think it writes an array of bytes of unknown size to a given address but I'm not entirely sure. 我在互联网上的某个地方找到了它,我认为它会将未知大小的字节数组写入给定地址,但是我不确定。 Can anyone explain what this does? 谁能解释这是什么?

And if this doesn't do what I think it does can someone help me produce a C++ function that takes an address and bytes as arguments and writes them at that address ie: 如果这不能满足我的要求,那么有人可以帮助我产生一个C ++函数,该函数将地址和字节作为参数,然后将其写入该地址,即:

myfunction(0xaddress, {0x10, 0x11, 0x12}); // something like this

The = operator of a vector will copy the contents of the rvalue (assuming rvalue is of type vector with the same type or a class which implements a casting to a vector from this same type). 向量的=运算符将复制右值的内容(假设右值是具有相同类型的vector类型或实现从该相同类型的向量转换的类的类)。

No address is being copied here only the contents. 没有地址仅被复制到这里。

If you want to write an x amount of bytes to an address, you have a number of options. 如果要向地址写入x个字节的字节,则有许多选择。

void myfunction(void* address, size_t count, const unsigned char data[])
{..}

const unsigned char bytes[] = {0x10, 0x11, 0x12};
myfunction (address, 3, bytes);

where the second argument, count, is the length of the array. 第二个参数count是数组的长度。
Or with a variadic function: 或具有可变函数:

void myfunction(void* address, size_t count, ...)
{..}

myfunction (address, 3, 0x10, 0x11, 0x12);

In all cases, you'll need to give the byte count explicitly though; 在所有情况下,您都需要明确给出字节数; the compiler can't deduce that from the data. 编译器无法从数据中推断出这一点。

If you want to use a vector, that's possible, but you'll need to populate the vector first before calling the function, which isn't as efficient. 如果要使用向量,则可以,但是在调用该函数之前,需要先填充向量,效率不高。

The only case where you wouldn't need to provide the count yourself is if none of the bytes would have value 0, then you could write 唯一不需要自己提供计数的情况是,如果所有字节的值都不为0,则可以写

void myfunction(void* address, const char* str)
{..}

myfunction (address, "\x10\x11\x12");

because you could use strlen! 因为您可以使用strlen!

Edit: Also, std::basic_string<unsigned char> would be worth looking into. 编辑:此外, std::basic_string<unsigned char>将值得研究。 But here too, it's not trivial to give this a value that contains \\x00 values. 但是在这里,给它一个包含\\x00值的值也不是一件容易的事。

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

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