Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I implemented a software stack, if I directly call below code in main function, there's NO error:

C#
typedef struct node
{
    int data;
    struct stack *next;
}NODE;

typedef NODE *STACK;
/*
... there are some code
*/
int main()
{
    STACK *st;
    STACK p = (STACK)malloc(sizeof(STACK));
    p->data = 8;
    if (empty(*st))
    {
        p->next = NULL;
        *st = p;
    }
    else
    {
         p->next = *st;
         *st = p;
    }
}


but when I put above code into a function and call this function, there is a run-time error (Windows): the program has stopped working

C#
typedef struct node
{
    int data;
    struct stack *next;
}NODE;

typedef NODE *STACK;
/*
... there are some code
*/
void push(STACK *st, int value)
{
    STACK p = (STACK)malloc(sizeof(STACK));
    p->data = 8;
    if (empty(*st))
    {
        p->next = NULL;
        *st = p;
    }
    else
    {
         p->next = *st;
         *st = p;
    }
}
int main()
{
    STACK *st;
    push(st, 8);
    
}


Thank you all!
Posted
Comments
Richard MacCutchan 19-Jun-12 3:39am    
Every time you call push() you are creating a new empty stack; I don't think that's what you meant to do.
OriginalGriff 19-Jun-12 3:44am    
That is the least of his problems! When was the last time you saw a stack node with a next pointer? :laugh:
Richard MacCutchan 19-Jun-12 4:02am    
That was the first thing that jumped out when I glanced at this, so I didn't bother to read the detail. OP is obviously trying to mix two tutorials: node and stack.
SVPro 19-Jun-12 5:48am    
No, It named STACK, but it's just a NODE of STACK!
Richard MacCutchan 19-Jun-12 7:54am    
Call it what you like, you obviously don't understand what you are doing.

1 solution

Perhaps you need to go back a bit and think about how variables and so forth work.

When you call your push function, the first thing you ar edoing is creating the stack onto which you will be pushing the value. So when you call it a second time, are you going to add variables to the same stack, or a different one? When you pop them, where are they going to come from?

And then you can look at what happens when you reference a pointer you haven't set yet...and start thinking about how a stack should work, and why it wouldn't have a next pointer at all!
 
Share this answer
 
Comments
SVPro 19-Jun-12 3:44am    
You mean I need to malloc for stack in main function?
OriginalGriff 19-Jun-12 3:50am    
That would help, yes.
Plus the stack should not be a local variabale at all, but global so that push and pop can both access it. And it doesn't need a next pointer in the node, but it does need a maximum size, and a check to make sure it isn't going to overflow, and a check to be sure it has an element before it gets popped, and...
You get the idea? A stack is a column of values where new ones go in at the top, and you can only remove the last element you added.
In a normal implementation you would have a top-of-stack pointer, and either a start address or a items count to allow you at check overflow / underflow. A stacked piece of info would not have a pointer to the next, because that is a list, not a stack!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900