converting "#define X" into "int X" with cudamalloc

i am trying to convert code 1 to code 2…
maybe i still don’t get C part. but, why is the second code not working?

isn’t ‘*’ for pointer?

any comments or advice please?

many thanks in well advance…

==========
code 1…

#define X 100

int c_d_test[X];
for(i=0; i<X; i++)
cudaMalloc((void **)&c_d_test[i], Y
sizeof(int));

==========
code 2…

int X = 100

int c_d_test;
c_d_test = (int ) malloc(Xsizeof(int));
for(i=0; i<X; i++)
cudaMalloc((void **)&c_d_test[i], Y
sizeof(int));

i am trying to convert code 1 to code 2…
maybe i still don’t get C part. but, why is the second code not working?

isn’t ‘*’ for pointer?

any comments or advice please?

many thanks in well advance…

==========
code 1…

#define X 100

int c_d_test[X];
for(i=0; i<X; i++)
cudaMalloc((void **)&c_d_test[i], Y
sizeof(int));

==========
code 2…

int X = 100

int c_d_test;
c_d_test = (int ) malloc(Xsizeof(int));
for(i=0; i<X; i++)
cudaMalloc((void **)&c_d_test[i], Y
sizeof(int));

Make that

int **c_d_test;

    c_d_test = (int **) malloc(X*sizeof(int *));

    for(int i=0; i<X; i++)

        cudaMalloc((void **)&(c_d_test[i]), Y*sizeof(int));

Make that

int **c_d_test;

    c_d_test = (int **) malloc(X*sizeof(int *));

    for(int i=0; i<X; i++)

        cudaMalloc((void **)&(c_d_test[i]), Y*sizeof(int));

by trial&error, i figured it must be
int **c_d_test;
c_d_test = (int **) malloc(X*sizeof(int *));

instead of

int *c_d_test;
c_d_test = (int ) malloc(Xsizeof(int));

but, why?

is it because i am going to use this temp array ‘c_d_test’ as pointers to pointers later in the code?

by trial&error, i figured it must be
int **c_d_test;
c_d_test = (int **) malloc(X*sizeof(int *));

instead of

int *c_d_test;
c_d_test = (int ) malloc(Xsizeof(int));

but, why?

is it because i am going to use this temp array ‘c_d_test’ as pointers to pointers later in the code?

Correct. c_d_test is a pointer on an array of elements of type (int*) hence it is of type (int**).

Correct. c_d_test is a pointer on an array of elements of type (int*) hence it is of type (int**).

code 1 was

int *c_d_test[100];

This defines and sets aside memory for an array of 100 int pointers.

You first tried

int *c_d_test;
c_d_test = (int ) malloc(Xsizeof(int));

which defines and sets aside memory for a single int pointer.

Thus you were successful with

int **c_d_test;
c_d_test = (int **) malloc(X*sizeof(int *));

code 1 was

int *c_d_test[100];

This defines and sets aside memory for an array of 100 int pointers.

You first tried

int *c_d_test;
c_d_test = (int ) malloc(Xsizeof(int));

which defines and sets aside memory for a single int pointer.

Thus you were successful with

int **c_d_test;
c_d_test = (int **) malloc(X*sizeof(int *));

thanks all.

its like a tail in my butt…circling around forever to catch the concept of “pointer” …ha ha ha

thanks all.

its like a tail in my butt…circling around forever to catch the concept of “pointer” …ha ha ha