I am trying to use memcpy
to pick out a specific range of data which I am interested, like using the syntax from Matlab array(100:200)
. However, when I tried to print out the result to check if the function did right or not, I got an error: Exception thrown at 0x00007FFF4A921399 (vcruntime140d.dll) in Examplefordebug.exe: 0xC0000005: Access violation writing location 0x0000000000000000.
Any thought to solve this problem? The result suppose to be two 5
from the array ch2Buffer
Here is the example code:
#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;
}