【发布时间】:2019-08-28 20:49:33
【问题描述】:
我正在做一个项目,从接收到的字节构造对象 MyClass。字节串由一个整数(4 个字节)和一个消息(5 个字节)组成。
我创建了一个模板类,以便能够轻松读取多种类型,还编写了一个模板特化函数来处理 char 数组的情况。
这是可复制粘贴到 main.cpp 文件中的代码。
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
template <typename T>
class ByteReaderT {
public:
static const char* read(const char* source, T& dest, size_t sz)
{
std::copy(source, source + sz, &dest);
return source + sz;
}
};
template<>
inline const char* ByteReaderT<char*>::read(const char* source, char*& dest, size_t sz)
{
return std::copy(source, source + sz, dest);
}
#define DATA_SIZE 5
struct MyClass {
int num;
char* data;
MyClass(): num(0), data(new char[DATA_SIZE]) {}
void read(const char* str) {
// data is still alive and well
str = ByteReaderT<int>::read(str, num, sizeof(int));
// data is gone (data = nullptr)
// I need to reallocate memory with data = new char[DATA_SIZE];
str = ByteReaderT<char*>::read(str, data, DATA_SIZE);
}
};
int main()
{
char received_arr[] = {
'\x01', '\0', '\0', '\0', // bytes for num
'r', 'e', 'c', 'v', ' ' // bytes for data
};
MyClass c;
char* ptr = nullptr;
c.read(received_arr);
std::cout << c.num << std::endl;
std::cout << std::string(c.data) << std::endl;
return 0;
}
但是在MyClass::read 函数中,我的数据指针在读取数字的前 4 个字节后重置为 nullptr。
而且我不知道为什么会这样。模板函数ByteReaderT<int>::read 不应触及数据指针。
我总是可以在读取 5 字节消息之前再次为 MyClass::read 中的数据分配内存,但这并不干净,因为我不应该这样做。
如果有人看到哪里出了问题,将不胜感激,因为我现在陷入困境。
【问题讨论】:
-
是否有任何不赞成投票的人参与其中,原因是什么?至少,这个问题有minimal reproducible example,现在在SO上很少见。
-
我首先将代码分成不同的块,然后意识到如果有人想编译它,复制粘贴会很痛苦。大概就是这个原因。
-
但是,它仍然可以通过 minimal reproducible example.. 我知道由于缺少 minimal reproducible example,我很快就会平局、投反对票和骂人,但你的例子看起来好的。显然,有些人在否决按钮上比我更快:/
-
@AlgirdasPreidžius 这不是最小的。它包含很多不相关的代码。
-
@L.F.如果您知道问题出在哪里,您可以创建minimal reproducible example,这只会产生上述问题。但是,到那时 - 你已经知道答案了,不需要问任何问题。
标签: c++ pointers templates null byte