title: What's the best way to put a c-struct in an NSArray?
body: <p>What's the usual way to store c-structures in an <code>NSArray</code>? Advantages, disadvantages, memory handling?</p>
  
  <p>Notably, what's the difference between <code>valueWithBytes</code> and <code>valueWithPointer</code>  -- raised by justin and catfish below.</p>
  
  <p><a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NumbersandValues/Articles/Values.html#//apple_ref/doc/uid/20000174-BAJJHDEG" rel="noreferrer">Here's a link</a> to Apple's discussion of <code>valueWithBytes:objCType:</code> for future readers...</p>
  
  <p>For some lateral thinking and looking more at performance, Evgen has raised the issue of using <code>STL::vector</code> in <strong>C++</strong>.</p>
  
  <p><em>(That raises an interesting issue: is there a fast c library, not unlike <code>STL::vector</code> but much much lighter, that allows for the minimal "tidy handling of arrays" ...?)</em></p>
  
  <p>So the original question...</p>
  
  <p>For example:</p>
  
  <pre><code>typedef struct _Megapoint {
      float   w,x,y,z;
  } Megapoint;
  </code></pre>
  
  <p>So: what's the normal, best, idiomatic way to store one's own structure like that in an <code>NSArray</code>, and how do you handle memory in that idiom?</p>
  
  <p>Please note that I am specifically looking for the usual idiom to store structs.  Of course, one could avoid the issue by making a new little class. However I want to know how the usual idiom for actually putting structs in an array, thanks.</p>
  
  <p>BTW here's the NSData approach which is perhaps? not best...</p>
  
  <pre><code>Megapoint p;
  NSArray *a = [NSArray arrayWithObjects:
      [NSData dataWithBytes:&amp;p length:sizeof(Megapoint)],
      [NSData dataWithBytes:&amp;p length:sizeof(Megapoint)],
      [NSData dataWithBytes:&amp;p length:sizeof(Megapoint)],
          nil];
  </code></pre>
  
  <p>BTW as a point of reference and thanks to Jarret Hardie, here's how to store <code>CGPoints</code> and similar in an <code>NSArray</code>:</p>
  
  <pre><code>NSArray *points = [NSArray arrayWithObjects:
          [NSValue valueWithCGPoint:CGPointMake(6.9, 6.9)],
          [NSValue valueWithCGPoint:CGPointMake(6.9, 6.9)],
          nil];
  </code></pre>
  
  <p>(see <a href="https://stackoverflow.com/questions/899600/how-can-i-add-cgpoint-objects-to-an-nsarray-the-easy-way">How can I add CGPoint objects to an NSArray the easy way?</a>)</p>