Memory allocation problem in Debug. Please help!

Hi,

What is the wrong with the belowcode in CUDA in Debug mode( NOT in EmuDebug )?

struct MyStr
{
int* iPtr;
float* fPtr;
}

MyStr* str;

CUDA_SAFE_CALL( cudaMalloc( (void**)&str, sizeof(MyStr) ); // This is working fine, no crash here, but the members of str is showing “Red” symbol ( i.e., not valid ) while debugging
CUDA_SAFE_CALL( cudaMalloc( (void**)&str->iPtr, sizeof(int)*20 ); // Here I got the crash, cuda stop working and running mode automatically stopped.

Now, please answer the questions…

  • Why the first statement is showing its members as not valid?
    [ ok, we can say that, if we allocating the memory only to str not for str members. ok, fine :) ]

  • But why the second statement is giving crash?

  • If I write something like the following…

    CUDA_SAFE_CALL( cudaMalloc( (void**)&str, sizeof(str) );

    int* intPtr = NULL;
    CUDA_SAFE_CALL( cudaMalloc( (void**)intPtr, sizeof(int)*20 );
    CUDA_SAFE_CALL( cudaMemcpy( &str->iPtr, intPtr, sizeof(int)*20, cudaMemcpyHostToDevice ) );

    then its working fine. why?
    So, do we have a copy in Host whenever we allocate memory in GPU? ( but I dont think so )

please answer above questions.

My guess would be the following - with the first malloc you allocate a block of device memory and assign its address to a pointer str, which itself resides in host memory. With the second malloc you are trying to allocate a memory from the host code and assign it to a str->iPtr which resides in device memory. This surely wouldn’t work in release, so probably it shouldn’t in debug either.

I got the same problem .