任何想法。我试过这个:
int *p = (int*)malloc(N*sizeof(int));
...//set the value of p
int8_t *q = reinterpret_cast<int8_t *>(p);但不起作用。我想得到结果q是连续的。但结果表明,如果p有4种元素:{1,2,3,4,5,6,7,8},则q为{1,0,0,0,0,0,0,0}
发布于 2017-03-31 09:43:23
您的代码不像您预期的那样工作,因为reinterpret_cast不会将这些int转换为int8_t,它只是将int的值表示为字节。与其使用malloc和reinterpret_cast,不如使用std::vector
//create an int vector of size N
std::vector<int> p (N);
//copy p contents into a vector of int8_t
std::vector<int8_t> q {p.begin(), p.end()}; 然后,如果您确实需要一个int8_t,您可以使用q.data(),但最好只使用向量或迭代器,而不是原始指针。
https://stackoverflow.com/questions/43137214
复制相似问题