Seg Fault when calling cudaMalloc! Can not set up structs with pointers inside!

Hi all,

I’m not going to claim to be the best programmer (by a long shot), but I can not figure out what I am doing wrong in my code. I am trying to initialize a variable that is of a struct type, and then initialize the pointers I have inside that struct. I whipped up a test code so I could isolate the effect, and I will post it below:

[codebox]#include

#include <stdlib.h>

#include <stdio.h>

#include <cuda.h>

using namespace std;

struct test

{

int a;

int *b;

};

int main()

{

test* x;

x=(test*)malloc(sizeof(test));

x->a=2;

x->b=(int*)malloc(10*sizeof(int));

for (int l=0; l<10; l++)

	x->b[l]=l;

cout <<"\nx->a= "<<x->a;

for (int l=0; l<10; l++)

	cout <<"\nx->b["<<l<<"]= "<<x->b[l];

cout<<endl;

test* x_d;

cudaMalloc((void**)&x_d,sizeof(test));

cudaMemcpy(&(x_d->a),&(x->a),sizeof(test),cudaMemcpyHostToDevice);

cudaMalloc((void**)&(x_d->b),10*sizeof(int*));

//Would add more once I get past this point!

}[/codebox]

The terminal output is as follows:

[codebox]nvcc test.cu -run

x->a= 2

x->b[0]= 0

x->b[1]= 1

x->b[2]= 2

x->b[3]= 3

x->b[4]= 4

x->b[5]= 5

x->b[6]= 6

x->b[7]= 7

x->b[8]= 8

x->b[9]= 9

Segmentation fault

[/codebox]

Thanks for any help you can give, I have been stumped by this for a bit too long now.

Adam

p.s. I forgot to mention: I have had no problems running any of the SDK samples, and so I believe the driver/toolkit/sdk are all installed just fine on my PC.

My setup is a core2duo e6600, Ubuntu 9.04, 2gb ram, 8800gts 312mb.

cudaMemcpy(&(x_d->a),&(x->a),sizeof(test),cudaMemcpyHostToDevice);

You’re dereferencing x_d, and that obviously won’t work since it’s on the device.

Ugh, yeah. I see what you meant.

I got what I need working now. Thanks for the help!