Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ vector on arduino takes up massive RAM footprint
    text
    copied!<p>Couldn't find a solution to this anywhere. Hoping to become more enlightened on this subject. </p> <p>I wanted to use a dynamic array of sorts for an arduino project. I came across a library for using vectors on the arduino platform <a href="https://github.com/maniacbug/StandardCplusplus" rel="nofollow">here</a>. I used a function to monitor free RAM on the arduino that I found <a href="http://www.controllerprojects.com/2011/05/23/determining-sram-usage-on-arduino/" rel="nofollow">here</a>. </p> <p>Here is an example of my code:</p> <pre><code>Serial.print("Starting RAM: "); Serial.println(freeRam()); Serial.println(); vector&lt;int&gt; intVector; Serial.print("Remaining RAM after intVector declaration: "); Serial.println(freeRam()); Serial.println(); vector&lt;char&gt; charVector; Serial.print("Remaining RAM after charVector declaration: "); Serial.println(freeRam()); Serial.println(); Serial.print("sizeof(intVector) = "); Serial.println(sizeof(intVector)); Serial.print("sizeof(charVector) = "); Serial.println(sizeof(charVector)); </code></pre> <p>And here's the output:</p> <pre><code>Starting RAM: 1684 Remaining RAM after intVector declaration: 1618 Remaining RAM after charVector declaration: 1584 sizeof(intVector) = 7 sizeof(charVector) = 7 </code></pre> <p>It seems that the intVector allocation took up a 66 byte chunk of the RAM. It appears the vector allots 32*2 + 2 = 66 bytes of memory for this. Likewise, the charVector allocation took up 34 bytes (32*1 + 2). It seems this pattern of allocating 32*<code>sizeof(type)</code> + change exists for other data types (such as char, float, etc). Note that <code>sizeof(int) = 2</code> on arduino. </p> <p>My problem is that the objects I would like to populate these vectors with are anywhere from 10-20 bytes large. With only 2 kB of RAM available on the ATMega328, I won't be able to run my program as it's currently designed. For an object <code>segment</code> with <code>sizeof(segment) = 16</code>, a vector eats up a 522-byte block of RAM. </p> <p>So my questions are:</p> <ol> <li><p>Why does the vector allocate 32 * sizeof(type) bytes of RAM when the size of the vector is only 7 bytes, despite its type?</p></li> <li><p>Is there a better way to use a sort of dynamic array on the arduino platform? </p></li> <li><p>Are there memory management techniques that may allow me to use vectors?</p></li> </ol> <p>Thanks, and sorry in advance if this question is a duplicate!</p> <p>EDIT:</p> <p>It seems that the vector is initialized with a capacity of 32. </p> <pre><code>intVector.capacity() = 32; </code></pre> <p>Attempting to use either </p> <pre><code>intVector.reserve(1); // or intVector.resize(1); </code></pre> <p>does not alter the capacity of the vector. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload