简体   繁体   English

C ++:转换向量 <char> 加倍

[英]c++: convert vector<char> to double

I am using vector<char> to send and receive data via socket. 我正在使用vector<char>通过套接字发送和接收数据。 In this vector I stored data of different types. 在此向量中,我存储了不同类型的数据。 Unsigned Integer and Doubles. 无符号整数和双打。 To decode the data from vector I am using copy function. 为了从矢量解码数据,我正在使用copy功能。

vector<char> myVector = ... smth;
double value = 0.0;
copy(myVector.begin(), myVector.begin() + sizeof(value), &value);

It works with Integer without problem. 它可以毫无问题地与Integer一起使用。 But... 但...

My problem is, that the compile gives out an error "free(): invalid pointer: 0x00000000006d0e30" . 我的问题是,编译给出错误"free(): invalid pointer: 0x00000000006d0e30" I checked, the problem is with the double value, not with the vector. 我检查过,问题出在双精度值,而不是向量。 I looked the address of double value it was ( 0x6d0e38 ). 我看了两倍值的地址是( 0x6d0e38 )。 Why the program tries to access the pointer backwards? 为什么程序尝试向后访问指针? I would be glad, if you can say me, what I am doing wrong. 如果您能说我,我会很高兴我做错了。 And is it the good way to decode message? 这是解码消息的好方法吗?

Thank you a lot. 非常感谢。

I guess that you need to cast the pointer accordingly to make ptr++ use the right size ( sizeof(char) , opposed to sizeof(double) ): 我想您需要相应地转换指针,以使ptr++使用正确的大小( sizeof(char) ,而不是sizeof(double) ):

vector<char> myVector = ... smth;
double value = 0.0;
std::copy(myVector.begin(), myVector.begin() + sizeof(value),
    reinterpret_cast<char*>(&value));

Don't do this. 不要这样 Send the string representation of the value through the socket. 通过套接字发送值的字符串表示形式。

std::stringstream ss;
double value = 0.0;
ss << value;

Then use ss.str() or if you really need a vector of char: 然后使用ss.str()或者如果您真的需要一个char向量:

std::vector<char> v(ss.begin(), ss.end());

-- edit -- -编辑-
If you really need to keep data binary, do 如果您确实需要保留数据二进制文件,请执行

  std::vector<char> v(sizeof(double));
  double val = 0.5;
  std::memcpy(&v[0],(char*)&val,sizeof(val));
  ...
  double* out = (double*)&v[0];
  std::cout << *out << std::endl;

It works with Integer without problem. 它可以毫无问题地与Integer一起使用。 But... 但...

It most certainly will not work for integers. 它肯定不会为整数的工作。 At least not for integers where sizeof(int) > 1 ! 至少对于sizeof(int) > 1整数不是! Because it will not write to just one integer, but spread the bytes in myVector over sizeof(T) integers, thus overwriting random memory. 因为它将不会只写入一个整数,而是将myVector的字节分布在sizeof(T)整数上,从而覆盖了随机内存。 (see nightcracker's answer) (请参阅“爆竹的答案”)

Please just use memcpy for this kind of copying: 请只使用memcpy进行这种复制:

vector<char> myVector = ... smth;
double value = 0.0;
assert(myVector.size() == sizeof(double));
memcpy(&value, &myVector[0], std::min(myVector.size(), sizeof(double)));
// as an alternative to the assert + std::min() above, you could also throw
// an exception if myVector.size() == sizeof(double) does not hold.
// (that's what I'd do if the size should always match exactly)

memcpy is made exactly for that kind of thing (copying raw memory), and I see no reason to use anything else here. memcpy正是针对这种情况(复制原始内存)而制作的,我认为这里没有理由使用其他任何东西。 Using std::copy does not make it better C++, especially when you're not doing it correctly. 使用std::copy不能使它成为更好的C ++,尤其是当您没有正确执行它时。 std::copy is for copying objects, not raw memory. std::copy用于复制对象,而不是原始内存。

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

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