title: Malloc, Variable Length Array or Static Array?
body: <p>I'm developing a program for a uC that reads in a 40 byte bit array via SPI. This 40 byte array is a test vector that's compared against a 'known good' test vector, which is stored on an SD Card.</p>
  
  <p>To find the anomalies/faults/errors, I XOR every byte of the received test vector with the stored data. This results in a bit array that has '1's where the faults lie. I can find the position of these '1's easily by using the following <a href="http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightModLookup" rel="nofollow">algorithm</a>.</p>
  
  <p>The trouble is storing the position of these '1's. Currently, I'm using Variable Length Arrays but the support for these is broken in GCC4.2 and so I'm not sure how reliable they would be in AVR-GCC.</p>
  
  <p>My next thought was to use <code>malloc</code>, but that's usually discouraged in embedded systems. Should I just declare a unsigned char array of length 320 and store a '1' when I find a bit set? Example: I find bits 4, 8, 10, 42 and 250 set in my bit array. I then set the corresponding elements to '1' to indicate that there were '1's detected at these positions. This would take up 320 bytes of my SRAM. Alternatively, I could declare the array as an int and store the actual position starting from the top of the array.</p>
  
  <p>Why I need the position of bits set? The SD Card contains another file which has information that corresponds to the position in the bit array. So, if a fault is discovered at position 24, the program can go and read the file and display information about everything it knows that's connected to position 24.</p>
  
  <p>NOTE: Some may wonder whether I need to read in the entire test vector at once. That is indeed the case.</p>