# Can't copy struct data from host to device

**URL:** <https://forums.developer.nvidia.com/t/cant-copy-struct-data-from-host-to-device/30248>\
**Category:** CUDA Programming and Performance\
**Created:** [July 27, 2013, 5:22pm UTC](https://forums.developer.nvidia.com/t/cant-copy-struct-data-from-host-to-device/30248 "2013-07-27T17:22:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![G\_fans](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@G\_fans](https://forums.developer.nvidia.com/u/G_fans)\
**Post date:** [July 27, 2013, 5:22pm UTC](https://forums.developer.nvidia.com/t/cant-copy-struct-data-from-host-to-device/30248/1 "2013-07-27T17:22:41Z")

</div>

Here is a simple code:

```auto
struct Point 
{ 
	float3 x;
	int3 y; 
	float z;
	int w;
};

int main() 
{ 
	Point *h_a = (Point*)malloc(sizeof(Point)); 
	h_a->x=make_float3(1.0,2.0,3.0); 
	h_a->y=make_int3(1,2,3); 
	h_a->z=10.0;
	h_a->w = 3;

	Point *d_a; 
	cudaMalloc((void**)&d_a,sizeof(Point));

	cudaMemcpy(d_a,h_a,sizeof(Point),cudaMemcpyHostToDevice); 

	return 0;
}

```

It’s just a simple code, but I can’t access device value pointed by d\_a, can someone help me?

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [July 27, 2013, 6:11pm UTC](https://forums.developer.nvidia.com/t/cant-copy-struct-data-from-host-to-device/30248/2 "2013-07-27T18:11:46Z")

</div>

I would suggest adding code to check the return status of every CUDA API call. It is entirely possible that CUDA failed to initialize, for example due to a driver version mismatch.

You state that you cannot access the device memory, what exactly does that mean? For example, there is no device code that operates on the data pointed to by d\_a. The code snippet above does not seem to have any obvious problems.

---

<div class="post-metadata">

**Author:** ![G\_fans](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@G\_fans](https://forums.developer.nvidia.com/u/G_fans)\
**Post date:** [July 27, 2013, 6:44pm UTC](https://forums.developer.nvidia.com/t/cant-copy-struct-data-from-host-to-device/30248/3 "2013-07-27T18:44:44Z")

</div>

> [@njuffa](#):
>
> I would suggest adding code to check the return status of every CUDA API call. It is entirely possible that CUDA failed to initialize, for example due to a driver version mismatch.
> 
> You state that you cannot access the device memory, what exactly does that mean? For example, there is no device code that operates on the data pointed to by d\_a. The code snippet above does not seem to have any obvious problems.

See the revised code below:

```auto
#include<cutil.h>
#include <stdio.h>

struct Point 
{ 
	float3 x;
	int3 y; 
	float z;
	int w;
};

int main() 
{ 
	Point *h_a = (Point*)malloc(sizeof(Point)); 
	h_a->x=make_float3(1.0,2.0,3.0); 
	h_a->y=make_int3(1,2,3); 
	h_a->z=10.0;
	h_a->w = 3;

	Point *d_a; 
	CUDA_SAFE_CALL(cudaMalloc((void**)&d_a,sizeof(Point)));

	CUDA_SAFE_CALL(cudaMemcpy(d_a,h_a,sizeof(Point),cudaMemcpyHostToDevice)); 

	if (d_a)
	{
		printf("d_a->z = %f", d_a->z);
	}

	return 0;
}

```

It will crash in the code line: printf(“d\_a-\>z = %f”, d\_a-\>z); I can’t dereference the pointer d\_a which is not NULL.And I didn’t get any error prompts after using cudaMalloc & cudaMemcpy.

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [July 27, 2013, 7:12pm UTC](https://forums.developer.nvidia.com/t/cant-copy-struct-data-from-host-to-device/30248/4 "2013-07-27T19:12:03Z")

</div>

Device and host (system) memory are different address spaces. You cannot dereference a pointer to device memory in host code. Likewise you cannot dereference a pointer to host memory in device code. There are ways of mapping host memory into the memory space of the device, but that is an advanced CUDA programming topic.

I would suggest reading the relevant sections of the CUDA C Programming Guide, or an introductory text, such as “CUDA by Example”.

---

<div class="post-metadata">

**Author:** ![G\_fans](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@G\_fans](https://forums.developer.nvidia.com/u/G_fans)\
**Post date:** [July 27, 2013, 7:24pm UTC](https://forums.developer.nvidia.com/t/cant-copy-struct-data-from-host-to-device/30248/5 "2013-07-27T19:24:50Z")

</div>

Thanks.

This helps me a lot. I will follow your advice.:)
