title: How can I get the size of an array from a pointer in C?
body: <p>I've allocated an "array" of <code>mystruct</code> of size <code>n</code> like this:</p>
  
  <pre><code>if (NULL == (p = calloc(sizeof(struct mystruct) * n,1))) {
   /* handle error */
  }
  </code></pre>
  
  <p>Later on, I only have access to <code>p</code>, and no longer have <code>n</code>. Is there a way to determine the length of the array given just the pointer <code>p</code>?</p>
  
  <p>I figure it <em>must</em> be possible, since <code>free(p)</code> does just that. I know <code>malloc()</code> keeps track of how much memory it has allocated, and that's why it knows the length; perhaps there is a way to query for this information? Something like...</p>
  
  <pre><code>int length = askMallocLibraryHowMuchMemoryWasAlloced(p) / sizeof(mystruct)
  </code></pre>
  
  <p>I know I should just rework the code so that I know <code>n</code>, but I'd rather not if possible. Any ideas?</p>