Home > Software design >  Write/read inside double pointer C
Write/read inside double pointer C

Time:02-02

I'm currently getting stuck writing and reading to a double pointer. It's the first time I'm using them and I'm a bit confused especially since it's a two dimensional array.

To simplify my code I have in my main :

Float32 **myTable;
myTable = malloc(sizeof(Float32) * NB_MAX_X * NB_MAX_Y);
myFunction(myTable);

NB_MAX_X and NB_MAX_Y are very large numbers and I had to initialize the variable in the heap memory.

In my function (I'm using HDF5 library) :

void myFunction (Float32 **myTable)
{
    file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
    dataset_id = H5Dopen2(file_id, "/SIGDAT/SIGDAT_VALUE", H5P_DEFAULT);

    // My problem start here
    status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, myTable);

    /* Show the dataset. */
    for (i = 0; i < NB_MAX_X; i  )
    {
        for (j = 0; j < NB_MAX_Y; j  )
        {
            printf("%f ", myTable[i][j]);
        }
        printf("\n");
    }printf("\n");

Just in case, I share with you the prototype of "H5Dread" :

H5_DLL herr_t H5Dread (
        hid_t dset_id,
        hid_t mem_type_id,
        hid_t mem_space_id,
        hid_t file_space_id,
        hid_t dxpl_id,
        void *buf /*out*/);

I don't know if I can write but when I read it I get this message: Access violation when reading location 0x0000000100000001

Thank for your help

CodePudding user response:

I see no reason to make myTable a double pointer. I suggest making it a pointer to the first row in your 2D array. This also matches the H5Dread signature that takes a void* to a flat (float) array in this case since you've specified using the H5P_DEFAULT transfer property.

herr_t H5Dread( hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id,
                hid_t file_space_id, hid_t xfer_plist_id, void * buf );

Example:

#include <stdlib.h>

void myFunction(Float32 (*myTable)[NB_MAX_X]) {
    // ...
    status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                     H5P_DEFAULT, myTable);
    // ...
}

int main() {   
    Float32 (*myTable)[NB_MAX_X] = malloc(NB_MAX_Y * sizeof *myTable);
    myFunction(myTable);    
}
  •  Tags:  
  • Related