# Lots of mallocs in my gnuradio block is it safe?

**URL:** <https://www.ruby-forum.com/t/lots-of-mallocs-in-my-gnuradio-block-is-it-safe/197640>\
**Category:** GNU Radio\
**Created:** [October 18, 2010, 6:17am UTC](https://www.ruby-forum.com/t/lots-of-mallocs-in-my-gnuradio-block-is-it-safe/197640 "2010-10-18T06:17:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 18, 2010, 6:17am UTC](https://www.ruby-forum.com/t/lots-of-mallocs-in-my-gnuradio-block-is-it-safe/197640/1 "2010-10-18T06:17:14Z")

</div>

Hi,  
I am writing the code for a gnuradio block and I use a lot of dynamic  
memory  
allocation. The code isn’t finished yet but I thought I would ask this  
question anyways. For an idea this is how the mallocs look like.

1. (204 + 5 + 204) gr\_complex arrays of size 2046 each
2. 1 double array of size 2046

The reason why I have this is most of the data allocated in these arrays  
is  
reused and I don’t have to calculate it again and again. i thought  
having  
this data ready when the block is initialized will speed up the  
processing.

Being pessimistic programmer I am circumspect about my method. What are  
your  
thoughts?

Thanks,  
John

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [October 18, 2010, 7:02am UTC](https://www.ruby-forum.com/t/lots-of-mallocs-in-my-gnuradio-block-is-it-safe/197640/2 "2010-10-18T07:02:59Z")

</div>

haha! I just realized that this is a great deal of memory used.  
250_2046_4.

I am so dumb. 🙂

I guess I will have to calculate this on each iteration i use that data.

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [October 18, 2010, 5:18pm UTC](https://www.ruby-forum.com/t/lots-of-mallocs-in-my-gnuradio-block-is-it-safe/197640/3 "2010-10-18T17:18:21Z")

</div>

It looks like only about 7 MB of memory… (204 + 5 + 204) \* 2046 \* 8 +  
2046

- 8 = 6,776,352 bytes. It probably won’t play too well with your CPU  
cache,  
but that still shouldn’t be too much, right?

Also, since you only need to calculate once, I assume you will be  
mallocing  
these arrays only once (e.g. in the constructor)… if you’re not  
mallocing  
every time general\_work is called, it should be even less of a problem.  
I  
myself haven’t had to malloc more than about 1MB of arrays in a block,  
though, but I think it may be worth trying out.

Kunal
