CudaMalloc for a pointer inside structure CUDA Programming

Hello,
I am using following code snippet:
++++++++++++++++++++++++++++++++++
struct obj_struct
{
double North[NuObj];
double East[NuObj];
double Dist[NuObj][NuObj];
double Area;
double* C2C;
double* Length;
};
struct obj_struct* d_Object;

cutilSafeCall(cudaMalloc((void**)&d_Object, sizeof(struct obj_struct)));
cutilSafeCall(cudaMalloc((void**)&(d_Object->C2C), NuObjNuSectsizeof(double)));
+++++++++++++++++++++++++++++++++++++++
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
GPU M2070
CentOS 5.6, 64 bit

Thanks,
Nikhil

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”.

Something like:

struct obj_struct host_Object[1];

cudaMalloc((void**)&(host_object->C2C), NuObj*NuSect*sizeof(double)));

// 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!