我正在为向量内uint8_t
指针内的向量内的boost::tuple
赋值。赋值本身似乎运行良好,但数据似乎在分配后立即释放,因为它往往被其他数据覆盖。
我在想packet* a = new packet;
应该分配一个新的packet
,而下面的.push_back(a)
应该复制指针并引用相同的值 - 显然我错了。
我希望有人看到我的错误;会感激任何帮助。
typedef boost::tuple<uint8_t, uint16_t, std::vector<uint8_t> > packet;
static void assignValueToMember(uint8_t& member){
member = 1;
}
static void assignValueToMember(uint16_t& member){
member = 2;
}
static void assignValueToMember(std::vector<uint8_t> member){
int maxIndex = 10;
for (int i = 0; i < maxIndex; i++){
uint8_t a;
assignValueToMember(a);
member.push_back(a);
}
}
static void assignValueToMember(std::vector<packet*>& member){
int maxIndex = 20;
for (int i = 0; i < maxIndex; i++){
packet* a = new packet;
assignValueToMember(boost::get<0>(*a));
assignValueToMember(boost::get<1>(*a));
assignValueToMember(boost::get<2>(*a));
member.push_back(a);
}
}
void Base::test(){
std::vector<packet*> a;
assignValueToMember(a);
packet* b = a.front();
std::vector<unsigned char> c = boost::get<2>(*b);
std::cout << c.front();
}
答案 0 :(得分:4)
static void assignValueToMember(std::vector<uint8_t> member)
您传递的是此向量的副本而不是引用。