# constant memory as pointer?

**URL:** <https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121>\
**Category:** CUDA Programming and Performance\
**Created:** [May 2, 2014, 4:09pm UTC](https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121 "2014-05-02T16:09:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ggeo](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ggeo](https://forums.developer.nvidia.com/u/ggeo)\
**Post date:** [May 2, 2014, 4:09pm UTC](https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121/1 "2014-05-02T16:09:02Z")

</div>

Hello,

Can I do this?

```auto
__constant__ float * myconst;

```

and then in host , something like:

```auto
cudaMemcpyToSymbol( myconst , inputF , sizeof(inputF)

```

Because if I define

```auto
__constant__ float myconst[100];

```

it works fine.

Can I use the constant memory as dymanically ?

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [May 2, 2014, 4:14pm UTC](https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121/2 "2014-05-02T16:14:49Z")

</div>

A pointer and an array are two different things. In the first case, only the pointer lives in constant memory. In the second case, the entire array lives in constant memory. The first example only allocates storage that is the size of a float pointer (so 4 bytes or 8 bytes). The second example allocates storage for the full array of float values.

The second example is probably what you want.

---

<div class="post-metadata">

**Author:** ![ggeo](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ggeo](https://forums.developer.nvidia.com/u/ggeo)\
**Post date:** [May 2, 2014, 4:26pm UTC](https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121/3 "2014-05-02T16:26:02Z")

</div>

Hmm , ok!

And if I have this:

```auto
__constant__ float Rows;

__constant__ float Cols;

```

Is there a way to do :

```auto
__constant__ float myconst[Rows*Cols]

```

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [May 2, 2014, 6:13pm UTC](https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121/4 "2014-05-02T18:13:46Z")

</div>

Not as far as I know. The myconst declaration you are showing implies a static allocation, which must be computable at compile time.

Note here:

[url][Programming Guide :: CUDA Toolkit Documentation](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#device-memory-qualifiers%5B/url%5D)

“ **shared** and **constant** variables have implied static storage.”

---

<div class="post-metadata">

**Author:** ![ggeo](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ggeo](https://forums.developer.nvidia.com/u/ggeo)\
**Post date:** [May 5, 2014, 7:13am UTC](https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121/5 "2014-05-05T07:13:44Z")

</div>

Ok , thank you!

---

<div class="post-metadata">

**Author:** ![Uncle\_Joe](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Uncle\_Joe](https://forums.developer.nvidia.com/u/Uncle_Joe)\
**Post date:** [May 6, 2014, 12:45am UTC](https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121/6 "2014-05-06T00:45:45Z")

</div>

If you’re using a CC 3.5 or later GPU, the constant memory is the same as the texture cache (called the read-only cache), I believe.

Then, instead of partitioning the data yourself, you can just let the hardware cache it in the read-only cache. You can do that by declaring your array as const **restrict** or by explicitly using ldg().

---

<div class="post-metadata">

**Author:** ![Uncle\_Joe](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Uncle\_Joe](https://forums.developer.nvidia.com/u/Uncle_Joe)\
**Post date:** [May 6, 2014, 12:46am UTC](https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121/7 "2014-05-06T00:46:18Z")

</div>

(delete me)

---

<div class="post-metadata">

**Author:** ![ggeo](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ggeo](https://forums.developer.nvidia.com/u/ggeo)\
**Post date:** [May 7, 2014, 11:15am UTC](https://forums.developer.nvidia.com/t/constant-memory-as-pointer/33121/8 "2014-05-07T11:15:33Z")

</div>

Ok, thanks!
