There are two problems in your code. The first issue is you are not initializing 'ImgMem'.
VOID* ImgMem;
// ^^^^^^ Points to whatever
If you need to use a buffer prior to copying the data to the image buffer you need to allocate memory for it.
int bufferSize = 640*480*2;
char* ImgMem = new char[bufferSize];
Make sure that you delete the memory once you are done with it or you will end up leaking memory.
delete[] ImgMem;
A better alternative would be to use std::vector
;
std::vector<char> ImgMem(bufferSize, 0);
memcpy(img->imageData, &ImgMem[0], ImgMem.size());
This way the memory is allocated, managed and freed by std::vector
instead of you having to do it every time.
The second problem is you are trying to copy more memory into the image buffer than it can hold.
memcpy(img->imageData, ImgMemory, (916*916));
This will copy 839,056 bytes when img->imageData
can only hold 307,200 bytes (assuming 8bpp).
[Edit: This answer does not take into account the attributes provided by the IplImage
structure. This includes size AND alignment which are fairly important here in regards to memcpy
. For more information on members of IplImage
see here]