# Using Device Pointers from different functions

**URL:** <https://forums.developer.nvidia.com/t/using-device-pointers-from-different-functions/20123>\
**Category:** CUDA Programming and Performance\
**Created:** [December 6, 2010, 1:39pm UTC](https://forums.developer.nvidia.com/t/using-device-pointers-from-different-functions/20123 "2010-12-06T13:39:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ygrene](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Ygrene](https://forums.developer.nvidia.com/u/Ygrene)\
**Post date:** [December 6, 2010, 1:39pm UTC](https://forums.developer.nvidia.com/t/using-device-pointers-from-different-functions/20123/1 "2010-12-06T13:39:05Z")

</div>

Hi!

I have some trouble using device pointers from different functions. I have a class and defined a (private) member which I want to use as a device pointer to a table in the class. However it only works if the pointer is only used in one function.

So something like

```auto
Class::InitTable()

{

XX = cudaMalloc(size);

setTable<<<...>>>();

}

Class::UseTable

{

DoSomething<<<...>>>(XX);

}

```

doesn’t work and the table seems just to be filled with random values. While on the other hand

```auto
Class::InitAndUseTable()

{

if (!XX) {cudaMalloc(size); setTable<<<...>>>();}

DoSomething<<<...>>>(XX);

}

```

works perfectly fine.

Is this a known problem with CUDA? What else could be the reason?

Thanks!

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [December 6, 2010, 1:58pm UTC](https://forums.developer.nvidia.com/t/using-device-pointers-from-different-functions/20123/2 "2010-12-06T13:58:44Z")

</div>

Are these functions potentially called by different host threads? Device pointers are generally not portable between CUDA contexts, and each host thread is implicitly given a different context in the runtime API.

---

<div class="post-metadata">

**Author:** ![Ygrene](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Ygrene](https://forums.developer.nvidia.com/u/Ygrene)\
**Post date:** [December 6, 2010, 2:14pm UTC](https://forums.developer.nvidia.com/t/using-device-pointers-from-different-functions/20123/3 "2010-12-06T14:14:06Z")

</div>

> [@](#):
>
> Are these functions potentially called by different host threads? Device pointers are generally not portable between CUDA contexts, and each host thread is implicitly given a different context in the runtime API.

Indeed they are, thanks for the hint.

I guess I’ll have to rewrite things to execude all the CUDA stuff from one thread.

---

<div class="post-metadata">

**Author:** ![tmurray](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tmurray](https://forums.developer.nvidia.com/u/tmurray)\
**Post date:** [December 6, 2010, 7:57pm UTC](https://forums.developer.nvidia.com/t/using-device-pointers-from-different-functions/20123/4 "2010-12-06T19:57:21Z")

</div>

Improving support for multithreaded applications is something we’re actively working on.
