title: EXC_BAD_ACCESS when passing a pointer-to-pointer in a struct?
body: <p>I've got a c-array of CGPoints in a struct.  I need to replace this array when another CGPoint is added.  I'd swear I'm doing this right and it seems to work fine a few times but eventually I'll get a <code>EXC_BAD_ACCESS</code>.  What am I missing?</p>
  
  <p>Here's the struct, which I've truncated to remove a lot of items that don't pertain.</p>
  
  <pre><code>typedef struct{
      CGPoint **focalPoints;
      NSUInteger focalPointCount;
      CGRect boundingRect;
  }FocalPoints;
  </code></pre>
  
  <p>Here's how I initialize it:</p>
  
  <pre><code>CGPoint *fPoints = (CGPoint *)malloc(sizeof(CGPoint));
  FocalPoints focalInfo = {&amp;fPoints, 0, rect};
  </code></pre>
  
  <p>Note that <code>focalInfo</code> is passed by reference to another function, like so: <code>anotherFunction(&amp;focalInfo)</code>.</p>
  
  <p>Now here's the function that replaces the Points array with a new one:</p>
  
  <pre><code>void AddFocalPoint (CGPoint focalPoint, FocalPoints *focal){
      if (focalPoint.x == CGFLOAT_MAX) return;
      if (!CGRectContainsPoint(focal-&gt;boundingRect, focalPoint)) return;
      int origCount = focal-&gt;focalPointCount;
      int newCount = origCount + 1;
      CGPoint *newPoints = (CGPoint *) malloc((newCount) * sizeof(CGPoint));
      for (int i = 0; i &lt; newCount; i++)
          newPoints[i] = (i &lt; origCount) ? *focal-&gt;focalPoints[i] : focalPoint; //error occurs here
      free(*focal-&gt;focalPoints);
      *focal-&gt;focalPoints = newPoints;
      focal-&gt;focalPointCount = newCount;
  }
  </code></pre>
  
  <p>The EXC_BAD_ACCESS error occurs in the above code on line 8: <code>newPoints[i] = (i &lt; origCount) ? *focal-&gt;focalPoints[i] : focalPoint;</code>. So what exactly am I doing wrong?</p>