简体   繁体   中英

3d matrix dynamic allocation error C2036: 'void *'

I have a MPI project and I'm supposed to convert it in cuda.

The problem is simple, the project use this code to allocate 3d matrix of different types.

void ***Matrix3Allocate(Point3 offset, Point3 size, int dataSize){
    void *matrix_data;
    void **matrix_data_ptr;
    void ***matrix;
    int nb_elements = Matrix3GetNbElements(size);

    message("Allocate matrix (%d,%d,%d) %d ko (plus pointeurs : %d ko)", 
            size.x, size.y, size.z, nb_elements * dataSize / 1024,
            size.x * ( 1 + size.y ) * sizeof(void*) / 1024);

    matrix = (void ***)secure_malloc(size.x * sizeof(void**));
    matrix_data_ptr = (void **)secure_malloc(size.x * size.y * sizeof(void*));
    matrix_data = (void *)secure_malloc(nb_elements * dataSize);

    matrix -= offset.x;
    for(int x = offset.x; x < offset.x + size.x; x++)
        matrix[x] = matrix_data_ptr + size.y * (x - offset.x) - offset.y;

    for(int x = offset.x; x < offset.x + size.x; x++)
        for(int y = offset.y; y < offset.y + size.y; y++)
            matrix[x][y] = matrix_data +
                    ((x - offset.x) * size.y * size.z +
                (y - offset.y) * size.z - offset.z) * dataSize;

    return matrix;
}

When compiling I have an error C2036: 'void *' unknown size on this part :

matrix[x][y] = matrix_data + ((x - offset.x) * size.y * size.z +
               (y - offset.y) * size.z - offset.z) * dataSize;

I understand why the compiler is screaming but this code was working before (probably with a different compiler) and I would like to change a minimum of code.

Could I make it work with a simple flag?

Is there a minimal modification that could make it compile without error?

The obvious (big) solution is to use c++ templates, but this was a C project so if there is an other (simpler) solution in C...

Thanks.

The problem is the addition to a void pointer ( matrix_data + ...) As far as I interpret your code, it should work by casting it to char * first. So try

matrix [x][y] = ((char *) matrix_data) + ...

The reason why this line gives an error:

matrix[x][y] = matrix_data + ((x - offset.x) * size.y * size.z +
                 (y - offset.y) * size.z - offset.z) * dataSize;

is that once you add a value to the pointer, the compiler needs to know the concrete type so that the correct "stride" is made to the next element. Since everything is void*, the compiler has no choice except to give you the error.

If your goal is to increment by sizeof(char) , then you need char pointers. The easiest fix would be to use a char * that points to matrix_data :

char *myMatrix = (char *)matrix_data;
matrix[x][y] = myMatrix + ((x - offset.x) * size.y * size.z +
               (y - offset.y) * size.z - offset.z) * dataSize;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM