title: keeping track of how much memory malloc has allocated
body: <p>After a quick scan of related questions on SO, I have deduced that there's no function that would check the amount of memory that malloc has allocated to a pointer. I'm trying to replicate some of std::string basic functionality (mainly dynamic size) using simple char*'s in C and don't want to call realloc all the time. I guess I'll need to keep track of how much memory has been allocated. In order to do that, I'm considering creating a typedef that will contain the string itself and an integer with the amount of memory currently allocated, something like this:</p>
  
  <pre><code>typedef struct {
      char * str;
      int mem;
  } my_string_t;
  </code></pre>
  
  <p>Is that an optimal solution, or perhaps you can suggest something that will bear better results? Thanks in advance for your help.</p>