# Allocate memory for object in cuda

**URL:** <https://forums.developer.nvidia.com/t/allocate-memory-for-object-in-cuda/22242>\
**Category:** CUDA Programming and Performance\
**Created:** [May 1, 2011, 2:38pm UTC](https://forums.developer.nvidia.com/t/allocate-memory-for-object-in-cuda/22242 "2011-05-01T14:38:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BehzadX](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@BehzadX](https://forums.developer.nvidia.com/u/BehzadX)\
**Post date:** [May 1, 2011, 2:38pm UTC](https://forums.developer.nvidia.com/t/allocate-memory-for-object-in-cuda/22242/1 "2011-05-01T14:38:56Z")

</div>

Dear All

I defined class like:

class element  
{  
public:  
int\* A;  
};

Then I defined pointers in main.cu  
element _e, dev\_e;  
.  
.  
.  
cudaMalloc( (void_)&(dev\_e-\>A), ne \* sizeof( int ) );  
.  
.  
.

BUT I recievd from compiler:  
“Warning: Cannot tell what pointer points to, assuming global memory space”

and also has memory segmentation fault in run time.

Please help me how I can allocate memory for an object containing a dynamic array…

Thank alot,  
BehzadX

---

<div class="post-metadata">

**Author:** ![B\_ron](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@B\_ron](https://forums.developer.nvidia.com/u/B_ron)\
**Post date:** [May 2, 2011, 7:55am UTC](https://forums.developer.nvidia.com/t/allocate-memory-for-object-in-cuda/22242/2 "2011-05-02T07:55:53Z")

</div>

> [@](#):
>
> class element
> 
> {
> 
> public:
> 
> int\* A;
> 
> };
> 
> Then I defined pointers in main.cu
> 
> element \*e, \*dev\_e;
> 
> .
> 
> .
> 
> .
> 
> cudaMalloc( (void\*\*)&(dev\_e-\>A), ne \* sizeof( int ) );

If dev\_e is a device variable, the pointer itself exists device memory, so you can’t dereference it in the host code. You have to allocate the member(s) in device memory, and have their handles as host pointers (still pointing to device memory, but the pointer is in host memory), and allocate the class/struct itself the same way.

Not sure what the best method is for copying all this to your device pointer afterwards, one way that definitely works is passing the pointers as parameters to a \<\<\<1, 1\>\>\> setter kernel that can work with the device variables. Or it might be simpler to pass it as a parameter to your kernel calls, and not use device variables at all.
