# CUDA Fortran - Dynamic Allocation Structure

**URL:** <https://forums.developer.nvidia.com/t/cuda-fortran-dynamic-allocation-structure/156472>\
**Category:** Legacy PGI Compilers (archived)\
**Tags:** cuda\
**Created:** [October 7, 2020, 10:32am UTC](https://forums.developer.nvidia.com/t/cuda-fortran-dynamic-allocation-structure/156472 "2020-10-07T10:32:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabius.94](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fabius.94](https://forums.developer.nvidia.com/u/fabius.94)\
**Post date:** [October 7, 2020, 10:32am UTC](https://forums.developer.nvidia.com/t/cuda-fortran-dynamic-allocation-structure/156472/1 "2020-10-07T10:32:53Z")

</div>

Hi everyone,  
I have a doubt about how to allocate arrays in a structure.

* * *

**Type** node\_type  
**real, dimension(3) ::** coord  
…  
**End type**

**Type** RBC\_type  
…  
**type(node\_type), allocatable, dimension(:) ::** node  
**End type**

* * *

And then in the main I have

**type(RBC\_type), DEVICE, allocatable, dimension(:) ::** RBC

Then, when I have to allocate the structure, I do the following:

**Allocate(RBC(N))**

and it seems to work.

But when I try to allocate the node:

**do i = 1, N**  
**allocate(RBC(i)%node(num\_nodes))**  
**end do**

I can compile, but when running I get this error: Segmentation fault (core dumped).  
I am allocating everything on the host side. Could you help me with this?

Thank you.

---

<div class="post-metadata">

**Author:** ![MatColgrove](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/matcolgrove/32/14047_2.png) [@MatColgrove](https://forums.developer.nvidia.com/u/MatColgrove)\
**Post date:** [October 7, 2020, 2:55pm UTC](https://forums.developer.nvidia.com/t/cuda-fortran-dynamic-allocation-structure/156472/2 "2020-10-07T14:55:25Z")

</div>

> [@fabius.94](#):
>
> I am allocating everything on the host side

That’s the problem. RBC is a device allocatable so accessing RBC(i) will cause a seg fault on the host since it’s not a host address.

Better to move the device attribute to “node” and then only use “node” within device code. Or make “node” a fixed size array so it doesn’t need to be allocated.
