I am using following code snippet:
struct obj_struct* d_Object;
cutilSafeCall(cudaMalloc((void**)&d_Object, sizeof(struct obj_struct)));
I get Segmentation Fault at the second “cudaMalloc” call. I understand that the second CudaMalloc call is “INVALID” as per CUDA programming specifications. I wanted to understand the internal details from CUDA architecture point of view that why it is so ?
I am using:
CUDA toolkit Release 4.0
CentOS 5.6, 64 bit
Your trying to access a device side pointer “d_Object” from the host which will give you an invalid pointer. Rule of thumb: never try to operate on a device side pointer from the host for other than passing it to functions that can make use of it ( ex my_kernel<<<< , >>>( device_ptr), cudaMalloc((void**)&my_ptr, size) , etc).
What you could do in above example is to first have a host side struct that gets its pointer set to device side buffer via cudaMalloc, in the next step you cudamemcpy your host side struct into “d_Object”.
struct obj_struct host_Object;
// Memcpy this to "d_Object"
(cudaMalloc((void**)&d_Object, sizeof(struct obj_struct))); // Allocate
cudaMemcpy(d_Object, host_object, SIZE, cudaMemcpyHostToDevice ); // MEMCPY
Hope this was helpful!