【发布时间】:2018-10-11 09:11:40
【问题描述】:
我想在 C++ 中将 strings、doubles 和 time_t 的一组值转换为 std::vector<unsigned char>。我为此使用memcpy,并在每次我有新值时调用它。我刚刚意识到我必须向后调用它才能在我的向量上以正确的顺序连接。转换所有值后,我想转换回来。我正在做的方式是每次再次调用memcpy 以将原始值转换回来。但是因为第一个之后的值在向量的中间,所以我无法正确地将它转换回来。
如何分别从向量中转换回所有值?我的代码在下面,输出也是。谢谢
int main(void) {
std::string lat = "lat->";
double latitude = 13.123456;
std::vector<unsigned char> result(sizeof(lat) + sizeof(latitude));
std::cout << "copying to the vector" << std::endl;
memcpy(result.data(), &latitude, sizeof(latitude)); // add string to the vector
memcpy(result.data(), &lat, sizeof(result.size()) + sizeof(lat)); // add double to the same vector
std::cout << "copied to the vector\n" << std::endl;
std::cout << "printing the vector" << std::endl;
for (int j = 0; j < result.size(); j++) {
std::cout << result[j];
}
std::cout << std::endl;
std::cout << "printed the vector\n" << std::endl;
// testing converting back ...................
std::cout << "printing back the original value" << std::endl;
double d;
std::string value;
// make sure the vector is the right size
if (result.size() != (sizeof(d) + sizeof(lat)))
throw std::runtime_error {
"Size of data in vector and float do not match" };
// copy the bytes into the float
memcpy(&value, result.data(), sizeof(value));
std::cout << value;
memcpy(&d, result.data(), sizeof(value) + sizeof(d));
std::cout << d << std::endl;
std::cout << "printed back the original value\n" << std::endl;
}
输出:
copying to the vector
copied to the vector
printing the vector
��(�lat->Pş0��(�
printed the vector
printing back the original value
lat->6.95297e-310
printed back the original value
Segmentation fault (core dumped)
【问题讨论】:
-
string.data(),不能直接memcpystd::string -
您的代码并没有像您认为的那样做任何事情,并且包含许多错误。我强烈建议不要尝试这样的字节操作。对于可能在堆上存储数据的类型(例如 std::string),它们将不可避免地失败。
-
在c++中你很少需要使用像
memcpy这样的低级函数(大多数时候只有当你需要与c apis或接近硬件的apis进行通信时),所以当你使用memcpy时,你应该考虑一下你是否真的做对了。 -
@appleapple
memcpy执行字节复制。问题是类型和大小而不是功能。 (尽管出于显而易见的原因你不叫 memcpy)