How can I "malloc" and "free" a 2D array in device?

Dear all,

I’d like to malloc and free a 2D array in device. My 2D array is like this:

a[0] = {0};
a[1] = {0, 0};
a[2] = {0, 0, 0};
a[3] = {0, 0, 0, 0};

How should I “malloc” and “free” such a array in device? I tried some times, but the program always crashed.

Thank you very much!!


It’s not clear what you are trying to do but… to malloc a 2D array on device you have to do this from the host and you can choose :

  • linear memory : cudaMallocPitch and then copy with cudaMemcpy2D from host to device

  • CUDA array : allocate : cudaArray, copy : cudaMemcpyToArray and then bind a texture to read from CUDA array (you can’t write to CUDA array)

Use cudaFree() to free allocated memory.

Read the programming guide for a good start…