title: Sequence Stack: uninitialized value was created by a heap allocation
body: <p>Just now I wrote a simple data structure <strong>Sequence Stack</strong> in c, and met a problem. </p>
  
  <pre><code>==8142== Use of uninitialised value of size 4
  ==8142==    at 0x408F4AB: _itoa_word (_itoa.c:195)
  ==8142==    by 0x40935EA: vfprintf (vfprintf.c:1629)
  ==8142==    by 0x4099EFE: printf (printf.c:35)
  ==8142==    by 0x40664D2: (below main) (libc-start.c:226)
  ==8142==  Uninitialised value was created by a heap allocation
  ==8142==    at 0x402BB7A: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==8142==    by 0x80484C9: sqstack_create (sqstack.c:24)
  ==8142==    by 0x80485D8: main (sqstack.c:84)
  ==8142== 
  ==8142== Conditional jump or move depends on uninitialised value(s)
  ==8142==    at 0x408F4B3: _itoa_word (_itoa.c:195)
  ==8142==    by 0x40935EA: vfprintf (vfprintf.c:1629)
  ==8142==    by 0x4099EFE: printf (printf.c:35)
  ==8142==    by 0x40664D2: (below main) (libc-start.c:226)
  ==8142==  Uninitialised value was created by a heap allocation
  ==8142==    at 0x402BB7A: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==8142==    by 0x80484C9: sqstack_create (sqstack.c:24)
  ==8142==    by 0x80485D8: main (sqstack.c:84)
  </code></pre>
  
  <p>The results show that bug might occur in line 24:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #include "status.h"
  
  #define MAX_SIZE 10
  
  typedef int ElemType;
  
  typedef struct{
      ElemType *base;
      ElemType *top;
      int      size;
  } sqstack_t;
  
  
  sqstack_t *sqstack_create(){
  
      sqstack_t *s = (sqstack_t *)malloc(sizeof(sqstack_t));
      if (s == NULL) {
          return ERROR;
      }
  
      //this line  24
      s-&gt;base = (ElemType *)malloc(sizeof(ElemType)*MAX_SIZE);
      if (s-&gt;base == NULL) {
          return ERROR;
      }
  
      s-&gt;top = s-&gt;base;
      s-&gt;size = 0;
  
      return s;
  }
  
  Status sqstack_push(sqstack_t *s, ElemType data){
  
      if (s-&gt;size &gt; MAX_SIZE) {
          printf("This stack is full!\n");
          return ERROR;
      }
  
      *s-&gt;top++ = data;
      s-&gt;size += 1;
  
      return OK;
  }
  
  
  ElemType sqstack_pop(sqstack_t *s){
  
      if (s-&gt;size == 0) {
          printf("This stack is empty!\n");
      return ERROR;
      }
  
      ElemType data;
  
      data = *s-&gt;top--;
      s-&gt;size -= 1;
  
      return data;
  }
  
  
  ElemType sqstack_top(sqstack_t *s){
  
      return *(s-&gt;top);
  }
  
  
  void sqstack_destroy(sqstack_t *s){
  
      s-&gt;top = NULL;
      free(s-&gt;base);
      free(s);
  }
  
  int main(void){
  
      sqstack_t *s;
  
      s = sqstack_create();
  
      sqstack_push(s, 10);
      sqstack_push(s, 20);
      sqstack_push(s, 30);
      sqstack_push(s, 40);
  
      printf("%d\n", sqstack_pop(s));
      printf("-%d-\n", sqstack_top(s));
  
      sqstack_destroy(s);
  
      return 0;
  }
  </code></pre>
  
  <p>I don't know how to fix it. I'm new to C and often confused with memory allocation and memory leaks. Could you recommend me any good materials or books? </p>
  
  <p>Thanks!</p>