2

I am still learning the C language. I have a problem to simulate a simple stack function, such as push, pop and so on. I found that the date and next are not initialized in the PTAIL. At that time, the program end. Is that counting as a memory leak?

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct Node{
    int date;
    struct Node * next;
}Node,*PNode;

typedef struct Stack{
    PNode pHead;
    PNode pTail;
}Stack;

void init(Stack *pS){  
    PNode n=(PNode)malloc(sizeof(Node));
    pS->pHead=n;
    pS->pTail=n;
    n->next=NULL;
}

void push(Stack *pS,int val){ 
    PNode p=(PNode)malloc(sizeof(Node));
    p->date=val;
    p->next=pS->pHead;
    pS->pHead=p;
}

void travel(Stack *pS){ 
    PNode p=pS->pHead;
    while(p->next){  
        printf("%d ",p->date);
        p=p->next;
    }
    printf("\n");
}

int main(void){

    Stack s;
    init(&s);  
    push(&s,1);
    travel(&s); 

    push(&s,1);
    travel(&s); 
    system("pause");
    return 0;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
bli
  • 35
  • 4
  • Debug your code for easier life, for everyone. – Maroun Feb 03 '14 at 08:11
  • Why do you create one node on `init`? Why not simply set `pHead` and `pTail` to `NULL` to mark stack as empty? – user694733 Feb 03 '14 at 08:18
  • [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Feb 03 '14 at 08:22
  • Hi @ᴍarounᴍaroun hmmm, pardon me my c skill is so so, the thing is I have debugged and then find the `next` and `date` are not initialized. – bli Feb 03 '14 at 11:05
  • Hi, @unwind , Thanks, I have seen your post, but i am using c++ compiler now and I have to cast in order to pass the error. – bli Feb 03 '14 at 11:09

2 Answers2

1

Every malloc should be paired with a free(). You didn't write/show your pop() function, but that's the place. If a memory buffer/object is not freed then it's a memory leak.

You allocated an empty first item in Init()m that's not needed.

Stacks usually do not need a tail, just a head. You also never update the tail...

egur
  • 7,830
  • 2
  • 27
  • 47
  • Thanks for your response. You remind me that the time i studied assembly, POP command in assembly means that clear the content the stack pointer point to. – bli Feb 03 '14 at 10:15
0

You allocate unnecessary node on init. What is more, you do not initialize date member of it. Remove the node creation:

void init(Stack *pS) {  
    pS->pHead = NULL;
    pS->pTail = NULL;
}

Casting is unnecessary on push.

PNode p = malloc(sizeof(Node));

Your travel function is checking for p->next even if p can be NULL. Change the while test:

void travel(Stack *pS) { 
    PNode p = pS->pHead;
    while(p) {  
        printf("%d ", p->date);
        p = p->next;
    }
    printf("\n");
}
user694733
  • 15,208
  • 2
  • 42
  • 68
  • Hi, @user694733 , I don't know why but if i change to "PNode p = malloc(sizeof(Node));" will give me error: a value of type "void *" cannot be used to initialize an entity of type "PNode" – bli Feb 03 '14 at 08:57
  • Are you using C++ compiler to compile C? – user694733 Feb 03 '14 at 09:05
  • yes @user694733 , how do you know it. Is that the problem? I thought it is compatible before. – bli Feb 03 '14 at 09:07
  • You can compile *some* C code with C++ compiler, but languages are sightly different (C++ has more strict type checing in this case). Using C++ compiler is not recommended if C compiler is available. – user694733 Feb 03 '14 at 09:09
  • Hi, @user694733 , i change the file to .c in visual studio(version 2012) but still see same error. I think I need to install a new IDE for C. – bli Feb 03 '14 at 09:36
  • I haven't used Visual Studio, but I *think* there should be a support for C too. Maybe it is some kind of project setting? – user694733 Feb 03 '14 at 09:52
  • @user3164230 If you just want to test this code, you can just add that cast to malloc and use C++ compiler. Just be aware that you might run into language differences sometimes. – user694733 Feb 03 '14 at 09:59
  • I get the value 1 and 11 as before, but I get a message from the compiler said that "FMUD.exe has triggered a breakpoint ". – bli Feb 03 '14 at 10:21
  • @user3164230 That would seem like a message from the debugger. You may have added breakpoint at some line. Just remove it. I can't help more with that as I am not familiar with VS. – user694733 Feb 03 '14 at 10:38
  • I checked the code but there is no breakpoints there, but very appreciate your response. – bli Feb 03 '14 at 10:44
  • Hi @user694733, I delete free() function at the last line and then the program comes no message. – bli Feb 03 '14 at 11:34