# Copying an array of structs to device memory

**URL:** <https://forums.developer.nvidia.com/t/copying-an-array-of-structs-to-device-memory/218038>\
**Category:** CUDA Programming and Performance\
**Created:** [June 17, 2022, 4:39pm UTC](https://forums.developer.nvidia.com/t/copying-an-array-of-structs-to-device-memory/218038 "2022-06-17T16:39:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![doscar.sole](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@doscar.sole](https://forums.developer.nvidia.com/u/doscar.sole)\
**Post date:** [June 17, 2022, 4:39pm UTC](https://forums.developer.nvidia.com/t/copying-an-array-of-structs-to-device-memory/218038/1 "2022-06-17T16:39:27Z")

</div>

Hi:

I’m facing trouble to copying an array of a struct (that contains a set of structs), and I’m a bit lost on how to solve it.  
So, I had following code that is working for copying a single struct item

```auto
static void _cinit(memory** n) {
  memory t;
  BigInteger w;

  cudaMalloc(n, sizeof(memory));
  BImemcpy(&w, 2);

  //device memory
  cudaMalloc(&t.vt, sizeof(BigInteger));

  //some more cudaMalloc

  cudaMemcpy(((BigInteger*)t.vt), &w, sizeof(BigInteger), cudaMemcpyHostToDevice);
  cudaMemcpy(*n, &t, sizeof(memory), cudaMemcpyHostToDevice);
}

int main(){
  memory* m = NULL;

  _cinit(&m);
}

```

so, I modified the code to make it work as an array

```auto
static void _cinit(memory** n, int cnt) {
  memory t;
  BigInteger w;
  int i;

  //cudaMalloc(n, sizeof(memory)); --> commented this line
  BImemcpy(&w, 2);

  //device memory
  cudaMalloc(&t.vt, sizeof(BigInteger));

  //some more cudaMalloc

  cudaMemcpy(((BigInteger*)t.vt), &w, sizeof(BigInteger), cudaMemcpyHostToDevice);
  //cudaMemcpy(*n, &t, sizeof(memory), cudaMemcpyHostToDevice); --> commented this line

  for (i = 0; i < cnt; i++) {
    cudaMalloc(&n[i], sizeof(memory)); 
    cudaMemcpy(n[i], &t, sizeof(memory), cudaMemcpyHostToDevice);
    printf("memory %p set result: %s\n", n[i], cudaGetErrorString(cudaGetLastError()));
  }
}

int main(){
  int i;
  memory* m = NULL;

  _cinit(&m, 4);

  for(i = 0;i < 4;i++){
    printf("received memory: %p\n", &m[i]);
  }
}

```

after an execution, I found that after first position of the array, pointers are not matching, making impossible to use any array position after 0.

```auto
memory 0000000701440A00 set result: no error
memory 0000000701440C00 set result: no error
memory 0000000701440E00 set result: no error
memory 0000000701441000 set result: no error
received memory: 0000000701440A00 --> same memory pointer
received memory: 0000000701440AA0 --> different memory pointer
received memory: 0000000701440B40
received memory: 0000000701440BE0

```

Obviously I’m doing something wrong, but I’m not able to find it… can anyone provide some hints of what’s happening here?

Thanks.
