title: Memory Allocation in Dynamic Linked List of Structs
body: <p>What's the difference in allocating memory with Malloc or Calloc in this example?<br>
  With Calloc the memory breaks, but with Malloc is fine!<br>
  My example is similar to (but with many values):<br><br>
  <a href="https://i.stack.imgur.com/cNXEY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cNXEY.png" alt="enter image description here"></a><p>
  If the code is wrong, how should I do it?</p>
  
  <pre><code>typedef struct {
      int ID, age;
  } person;
  
  typedef struct {
      person *person;
      struct NO *next;
  } NO;
  
  ...   // with calloc, the memory breaks
        // with  (NO*)malloc(sizeof(NO))  it´s fine
  
  NO *p1, *px1, *px2;
  
  px2 = (NO*)calloc(1, sizeof(NO));
  p1 = px2;
  px2-&gt;person-&gt;ID = 1; px2-&gt;person-&gt;age = 30;
  px2-&gt;next = NULL;
  px1 = px2;
  
  px2 = (NO*)calloc(1, sizeof(NO));
  px2-&gt;person-&gt;ID = 2; px2-&gt;person-&gt;age = 20;
  px2-&gt;next = NULL;
  px1-&gt;next = px2;
  </code></pre>
  
  <p>...</p>