<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 3 年前。
标签 c++ c++builder
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 3 年前。
我正在尝试在 C++ Builder 社区版中使用以下来自 sflowtool.c 的代码,但收到访问冲突。
uint32_t TForm3::getData32_nobswap(SFSample *sample)
{
uint32_t ans = *(sample->datap)++; // HERE I GET THE ERROR
if((uint8_t *)sample->datap > sample->endp) {
SFABORT(sample, SF_ABORT_EOS);
}
return ans;
在 &sample 传递给上面的函数之前,它有这个
SFSample sample;
memset(&sample, 0, sizeof(sample))
sample.rawSample = (uint8_t *) AData[0];
sample.datap = (uint32_t *) sample.rawSample;
sample.endp = (uint8_t *) sample.rawSample + sample.rawSampleLen;
sample.datagramVersion = getData32(&sample);
其中 AData 的类型为“const TIdBytes AData”
任何人都可以给我一些指示来理解原因吗?与 C 相比,C++ 处理上述代码有什么不同吗?
最佳答案
你的意思是:
sample.rawSample = (uint8_t *) &AData[0];
注意附加的 &
。这意味着您将 rawSample
设置为 AData
数组中第一个元素的地址,而不是将 AData
视为指针数组并采用它的第一个指针。
关于c++ - 在 C++ 中使用一些 C 代码并出现访问冲突错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685870/