title: ANSI C Memory allocation for pointer to struct throws non-fatal run-time error
body: <p>I am using ANSI C code, generated from a code generator that produces several layers of nested <code>struct</code> as well as function prototypes with argument lists that use pointers to the top layer <code>struct</code> for passing or accessing data located in the inner mmost <code>struct</code>. </p>
  
  <p>Because the function prototypes pass pointers to struct, the application must allocate memory to access or write data to the inner most struct members.  I am seeing the problem when attempting to allocate memory for the pointer to the second nested struct.</p>
  
  <p>The actual error message I get is a non-fatal, run-time:
  "Not enough space for casting expression to 'pointer to struct data'."</p>
  
  <p>I am not explicitly casting anything, so I suspect the implementation of <code>malloc()</code> may have an assert that generates the message when it sees some condition.   The wording of this error may be specific to my environment (I am using LabWindows/CVI) but I would be interested in hearing of results of other ANSI C compilers as well.</p>
  
  <p>Here is a simplified, complete, code snippet that should compile, build and run (up to the error location, which is commented in-line)  </p>
  
  <p>I would appreciate comments on the cause of my error, and suggestions on how to fix it.</p>
  
  <pre><code>#include &lt;ansi_c.h&gt; //specific to LabWindows/CVI - change as needed for your environment
  
  struct request
  {
      struct data *pData; 
  };
  
  struct data
  {
      char *wsDate;   
      char *wsDuration;
      char *wsFailures;
      int __sizeM_Details;    
      struct details *M_Details;
  };
  
  
  struct details
  {
      char *wsStep;   
      char *wsTestDesc;   
      char *wsLowLim; 
  };
  
  typedef struct request REQUEST;  // from mtdf function prototype request argument (4)
  typedef struct details DETAILS; // member of REQUEST - _ns1__MTDFData_MTDFDetail  
  
  void allocate(REQUEST *a, int numRecords);
  void freemem(REQUEST *c, int numRecords);
  
  int main(void)
  {
      REQUEST b, *pB;
  
      pB = &amp;b;
  
      allocate(pB, 10);
      freemem(pB, 10);
      return 0;   
  }
  
  void allocate(REQUEST *c, int numRecords)
  {
      DETAILS m_Param;
      REQUEST b;
      struct data d;
  
      size_t size_c = sizeof(c);
      c = malloc(size_c); //4 bytes
  
      size_t size_c_data = sizeof(c-&gt;pData);
      c-&gt;pData = malloc(size_c_data);  //Breaks here - this is just a pointer, 
                                       //should it not just allocate 4 bytes
                                       //and continue?
      //  Actual error message: 
      //  "Not enough space for casting expression to 'pointer to struct data'."
      c-&gt;pData-&gt;wsDate = calloc(80, sizeof(char));
  
      c-&gt;pData-&gt;__sizeM_Details = numRecords;
      c-&gt;pData-&gt;M_Details = calloc((numRecords + 1) , sizeof(m_Param)); 
  
  }
  
  void freemem(REQUEST *c, int numRecords)
  {
      free(c-&gt;pData-&gt;M_Details);
      free(c-&gt;pData-&gt;wsDate);
      free(c-&gt;pData);
      free(c);
  }
  </code></pre>