【发布时间】:2020-12-22 14:06:15
【问题描述】:
我正在尝试使用memcpy 来挑选我感兴趣的特定数据范围,例如使用Matlab array(100:200) 中的语法。但是,当我尝试打印结果以检查功能是否正确时,出现错误:Exception thrown at 0x00007FFF4A921399 (vcruntime140d.dll) in Examplefordebug.exe: 0xC0000005: Access violation writing location 0x0000000000000000. 有没有想过解决这个问题?结果假设是数组ch2Buffer中的两个5
示例代码如下:
#include <iostream>
#include <stdio.h>
#include <string.h>
int main()
{
int i,n;
const int u32Size = 10;
float* ch1Buffer = NULL;
double* ch2Buffer = NULL;
double* ch2newBuffer = NULL;
int pBuffer[u32Size] = {10,2,10,2,10,5,10,5,10,2};
int* pi16Buffer = pBuffer;
ch1Buffer = (float*)calloc(u32Size, sizeof* ch1Buffer);
ch2Buffer = (double*)calloc(u32Size, sizeof* ch2Buffer);
// De-interveal the data to ch1Buffer and ch2Buffer
for (i = 0; i < u32Size/2; i++)
{
ch1Buffer[i] += pi16Buffer[i * 2];
ch2Buffer[i] += pi16Buffer[i * 2 + 1];
}
// Use memcpy to pick out the data we are interested
memcpy(ch2newBuffer, &ch2Buffer[2], 2 * sizeof(ch2Buffer[0]));
// Print out to check the result
for (i = 0; i < 3; i++) {
printf("%f ", ch2newBuffer[i]);
}
free(ch1Buffer);
free(ch2Buffer);
return 0;
}
【问题讨论】:
-
memcpy(ch2newBuffer,- 您正在复制到一个为 NULL 的指针变量,因为目标从未被分配。因此你会得到一个 NULL 指针解引用。 -
那么您的意思是
ch2newBuffer必须声明为其他格式吗?
标签: arrays c sorting printf memcpy