# Problems on memory allocation

**URL:** <https://forums.developer.nvidia.com/t/problems-on-memory-allocation/26447>\
**Category:** CUDA Programming and Performance\
**Created:** [April 23, 2012, 3:45am UTC](https://forums.developer.nvidia.com/t/problems-on-memory-allocation/26447 "2012-04-23T03:45:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elguepardo](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@elguepardo](https://forums.developer.nvidia.com/u/elguepardo)\
**Post date:** [April 23, 2012, 3:45am UTC](https://forums.developer.nvidia.com/t/problems-on-memory-allocation/26447/1 "2012-04-23T03:45:45Z")

</div>

Hi all,

currently I faced some problems when I try to run my cuda code. I have on program which needs to compute an array of 96_96_96, but sometimes the result will diverge.

However, when I find out my result is diverging, if I run another simple program with a array of 8112\*8112 and then back to execute the former code, the result won’t diverge.

It just like every time when I need to run my code, I need to use another code to free memory space on my device, otherwise I cannot get my result correct.

I have no idea why this problem will happen, since I just use cudaMalloc() and cudaFree() for my memory allocation and release, please help me on this, thanks a lot.

---

<div class="post-metadata">

**Author:** ![pasoleatis](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pasoleatis](https://forums.developer.nvidia.com/u/pasoleatis)\
**Post date:** [April 23, 2012, 5:21am UTC](https://forums.developer.nvidia.com/t/problems-on-memory-allocation/26447/2 "2012-04-23T05:21:49Z")

</div>

There is a problem in the code. Memory access is out of bonds. add the flags -g -G to the compiling line and then run the program with cuda memcheck.

---

<div class="post-metadata">

**Author:** ![elguepardo](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@elguepardo](https://forums.developer.nvidia.com/u/elguepardo)\
**Post date:** [April 24, 2012, 2:41am UTC](https://forums.developer.nvidia.com/t/problems-on-memory-allocation/26447/3 "2012-04-24T02:41:47Z")

</div>

hi, pasoleatis

thanks for your reply, I tried it today with compiling my code first with

nvcc -arch=sm\_21 -O3 -g -G -o executefile mycode.cu

and then used cuda-memcheck to check like

cuda-memcheck -c ./executefile

However, the report is

=== ERROR SUMMARY: 0 errors

I think it means there is no error in my source code, did I miss something to do or there is really no mistake in my code?

---

<div class="post-metadata">

**Author:** ![pasoleatis](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pasoleatis](https://forums.developer.nvidia.com/u/pasoleatis)\
**Post date:** [April 24, 2012, 10:30am UTC](https://forums.developer.nvidia.com/t/problems-on-memory-allocation/26447/4 "2012-04-24T10:30:33Z")

</div>

Yes. It appears there are no memory problem (outbonds). Did you run the program with 96_96_96? You mentioned the problem does not appear always, so you should run your program until you get a divergence. Post also the output when you get a divergence. Usually when an address is not initialise I get a very large number, negative most of the time.

---

<div class="post-metadata">

**Author:** ![elguepardo](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@elguepardo](https://forums.developer.nvidia.com/u/elguepardo)\
**Post date:** [April 24, 2012, 10:56am UTC](https://forums.developer.nvidia.com/t/problems-on-memory-allocation/26447/5 "2012-04-24T10:56:24Z")

</div>

thank you a lot, pasoleatis, I think the reason causing my problem is just about initialization.

currently I can run my code with a 192_192_192 array size, and what I did was to initialize all variables I declared, which I did do previously.

thanks again.

---

<div class="post-metadata">

**Author:** ![pasoleatis](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pasoleatis](https://forums.developer.nvidia.com/u/pasoleatis)\
**Post date:** [April 24, 2012, 11:44am UTC](https://forums.developer.nvidia.com/t/problems-on-memory-allocation/26447/6 "2012-04-24T11:44:32Z")

</div>

After you allocate memory on gpu for an array you can set it all to zero with a cudaset function. You should check why does program read an address which was not written first.
