我有一个 u_char 数组,每次我进入循环时都会改变,还有一个结构体,其成员为 u_char 数组。我正在尝试创建一个结构向量,其中包含 u_char 数组的所有迭代。但是我得到一个段错误(当我使用 memcpy 时)或相同的值重复(当我不这样做时)。帮助?这是代码
u_char * data; //contains the data I want to copy from each iteration
unsigned char mem1[bytes];
for(int i=0; i<bytes; i++) {
mem1[i] = data[i];
}
struct load l1; //contains one member (pload of type u_char*)
l1.pload = mem1; //this gives me the same value in all elements of the vector
//I also tried using memcpy, like this:
//memcpy(&l1.pload, mem1, bytes); //this gave me a segfault
recd.push_back(l1) //recd is a std::vector<load>
我这样打印出来:
for(std::vector<load>::iterator it1 = recd.begin(); it1 != recd.end(); ++it1) {
cout<<"\nhere\n"<<endl;
cout<<it1->pload<<endl;
}