• Uncategorized

About c++ : Copying-a-MAT-image-to-specific-address-in-c

Question Detail

I’m trying to copy a MAT image loaded in from a file to a specific address location. I have the following code

int main()

    cv::Mat inImg =    cv::imread("M6_traffic.jpg");

    //Data point copy
    unsigned char * pData = (unsigned char *)inImg.data;
    unsigned char * Dest   = (unsigned char *)0x0f000000;;

    int width = inImg.rows;
    int height = inImg.cols;

    //data copy using memcpy function
    memcpy(Dest, pData, sizeof(unsigned char)*width*height*3);


But when I run this it always crashes, any idea why? and is there a better why to do this? My end goal is to able to copy a image data to specific address in a Linux based system

Question Answer

propably there is no allocated memory on address 0x0f000000.
and you cannot force this to be a valid address

Allocate the right size of memory with malloc and it will work.
The OS then determines your address space and returns a pointer to it.

unsigned char* Dest = malloc(sizeof(pData));
memcpy(Dest, pData, sizeof(pData))

0x0f000000 is essentially a null pointer. What you’re trying to do is copy data into a memory address that has not been set with memcpy(Dest, pData, sizeof(unsigned char)*width*height*3);.

You should allocate memory for Dest before your memcpy statement. You can do this with either malloc or calloc. These function calls are described in the following links. Hope this helps!



You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.