0

I am having a problem with passing a pointer to a function. When the function returns the pointer seems to be different than what it is in the function.

So I pass a pointer to the function which gets raw image data which should then be stored in the memory referenced by the pointer.

If I then pass the pointer from inside the raw image function to a function to create a JPEG file from the raw data then it works correctly.

If I first wait for the raw image function to finish and then call the JPEG function using the pointer I passed to the raw image function then it fails to create the image.

A simplified version of the code is below:

int getRawImage(unsigned char *pBuffer);
int writeJPEGBFile(unsigned char *idata, char *ofile);

int main(int argc, char** argv) {
    unsigned char *rawData = NULL;
    char filename[MAXPATHLEN] = "/home/user/tst/img.jpg";

    getRawImage(rawData);
    // This does not work
    writeJPEGBFile(rawData, filename);
    free(rawData);
    return 0;
}

int getRawImage(unsigned char *pBuffer) {
    void *hDevice;

    hDevice = scanOpenDevice();
    // Removed code for simplification
    scanGetFrame(hDevice, pBuffer, NULL)
    scanCloseDevice(hDevice);
    // This Works!!
    //char filename[MAXPATHLEN] = "/home/user/tst/img.jpg";
    //writeJPEGBFile(pBuffer, filename);
    return 0;
}

int writeJPEGBFile(unsigned char *idata, char *ofile) {
    // JPEG code goes here
    return 0;
}

My question is what am I doing wrong and how can I pass the rawData pointer to the writeJPEGBFile() function successfully in the main() function?


The definition for scanGetFrame() is as follows:

typedef void *FTR_PVOID;
FTR_API_PREFIX FTR_BOOL FTR_API ftrScanGetFrame( FTRHANDLE ftrHandle, FTR_PVOID pBuffer, PFTRSCAN_FRAME_PARAMETERS pFrameParameters );

The scanGetFrame() function comes from a 3rd party library that I am linking with so I will not be able to change the definition.

Blazanor
  • 175
  • 8
  • How does `scanGetFrame` add the data? – juanchopanza Oct 08 '14 at 06:50
  • 1
    have you tried printing the pointer variables in both cases just before they get passed to the `writeJPEGBFile` ? – Hrishi Oct 08 '14 at 06:51
  • It looks like `scanGetFrame` is a macro that takes tha address of `pBuffer` or some such. Otherwise I don't see this working. Could you post the difinition of `scanGetFrame`? – M Oehm Oct 08 '14 at 06:51
  • 1
    Who is in charge of allocating the memory pointed by rawData ? You have a free, but I can't see any malloc in the code. If getRawImage has to change the pointer, then it must get a char** instead of a char*. – Claudio Oct 08 '14 at 06:52
  • The memory is allocated in getRawImage() as follows: pBuffer = (unsigned char *) malloc(ImageSize.nImageSize); – Blazanor Oct 08 '14 at 09:15

2 Answers2

1

Given that rawData is a null pointer in main(), you almost certainly need to revise the interface to getRawImage() so that it takes a char ** and you pass &rawData to it. You also need to think about how the calling code will know how big the data is.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Can you suggest a possible solution considering that scanGetFrame() is expecting parameter 2 to be char *? This is my first C project though I have worked with PHP, Java and C# for years, but I am still getting used to C pointers and memory allocation. Thank you for your time. – Blazanor Oct 08 '14 at 20:32
  • I would expect to interpret `FTR_PVOID` as 'pointer to void' or `void *`, but it might be mapped to `char *` instead. Since I know nothing about the library, I'm sorry, I can't help any more. You need to know what the function does, and how its interface is supposed to work. Without the manual, nothing doing. A Google search for `ftrScanGetFrame` seems to suggest Futronics Fingerprint Reader as the library, but you should identify whether that's what you're using and the canonical URL for the library and its documentation. – Jonathan Leffler Oct 09 '14 at 03:07
0

I managed to work it out. Thanks to all for the pointers which led me to the solution:

int getRawImage(unsigned char *pBuffer);
int writeJPEGBFile(unsigned char *idata, char *ofile);

int main(int argc, char** argv) {
    unsigned char *rawData; // Removed the NULL assignment
    char filename[MAXPATHLEN] = "/home/user/tst/img.jpg";

    // Set the size of rawData - loadImageSize() sets the value of the ImageSize class variable.
    loadImageSize();
    rawData = (unsigned char *) malloc(ImageSize.nImageSize);

    getRawImage(rawData);
    // This works now
    writeJPEGBFile(rawData, filename);
    free(rawData);
    return 0;
}

int getRawImage(unsigned char *pBuffer) {
    void *hDevice;

    hDevice = scanOpenDevice();
    // Removed code for simplification
    scanGetFrame(hDevice, pBuffer, NULL)
    scanCloseDevice(hDevice);
    return 0;
}

int writeJPEGBFile(unsigned char *idata, char *ofile) {
    // JPEG code goes here
    return 0;
}
Blazanor
  • 175
  • 8