title: Declaration and Allocation
body: <p>I have some doubts about C allocations.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  
  typedef struct MyStruct {
      char CharsInMyStruct[50];
  } MyStruct;
  
  int main(void) {
  
      struct MyStruct * s;
  
      s = malloc(100 * sizeof *s);
  }
  </code></pre>
  
  <p>I was told that the allocation is "smart", because the space is not "really" (physically) allocated until I use the memory. Is the memory physically allocated once I use the first element of MyStruct or it's allocated when I use each element?</p>
  
  <p>I thought that if I need to dynamically read a file and copy each row in a dynamic struct, I could just allocate for 100 elements, then realloc for the correct dimension after reading the file, so I don't have to read two times to previously know how much to allocate or to realloc for each row. Is that possible / allowed? Is it a good solution?</p>
  
  <p>Edit: sorry, I was too concerned about theory and I forgot to add a decent code. Maybe the question is not clear enough, but I don't need to know how to code what I'm thinking about; I just need to know how the memory is allocated.</p>