Cuda dynamic allocating memory

I use 150x150 grid size, 150 block size.

When I try to run

int *array = new int[arrayLen];
int *array2 = new int[array2Len];
for(int i=0;i<arrayLen;i++)
array[i]= oldArray[i];

I get a Exception in thread “main” jcuda.CudaException: CUDA_ERROR_ILLEGAL_ADDRESS.

If I don’t try to access array[i] I don’t get any problem.
So, it can allocate memory but it cannot access it?

You should read the programming guide concerning dynamic memory allocation:

http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#dynamic-global-memory-allocation-and-operations

The heap size is limited. If you exceed that, your new allocation will fail and will return a NULL pointer. If you attempt to use that NULL pointer anyway you will get CUDA_ERROR_ILLEGAL_ADDRESS, since NULL is an illegal address.

As a safety check, test the returned pointer for NULL before using it.

If you are getting NULL, i.e. running out of memory, you can increase the heap size.

Thank you!