Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    I cannot duplicate your results. I still cannot get it to fail. Could you please give me a step-by-step test case that causes my version of the program to crash so that I can run the same test case?

    I noticed that you had made extensive changes to the program, the only one of which you made any claims about being the typedef, which has no effect.

    And BTW, your code won't even compile:
    C:stack>gcc -Wall bobs.c -o bobs
    bobs.c: In function `update':
    bobs.c:156: parse error before `int'
    bobs.c:165: `n' undeclared (first use in this function)
    bobs.c:165: (Each undeclared identifier is reported only once
    bobs.c:165: for each function it appears in.)
    bobs.c:169: `i' undeclared (first use in this function)
    bobs.c:173: `flag' undeclared (first use in this function)

    C:stack>
    Last edited by dwise1_aol; February 25th, 2013 at 02:30 PM.
  2. #17
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by BobS0327
    Well, your app consistently crashes on executing the pop function and I can cause peep and display to crash if I go back between pop adn display/peep four or five times.
    As I already said, I tried what you describe and my version of the program simply refused to crash, plus it maintained the stack correctly. For example, here's my most recent test case (please note that this is a format for the test case I had requested from you):
    pushed 9 items
    display -- all 9 are there
    pop
    display -- top item gone, 8 are left
    pop
    display -- top item gone, 7 are left
    peek 5 -- 5th item down displayed
    pop
    display -- top item gone, 6 are left
    push
    display -- new item on top, 7 items present
    pop
    display -- top item gone, 6 are left
    peep 8 -- "wrong input", since there are fewer than 8 items
    pop
    display -- top item gone, 5 are left
    pop
    display -- top item gone, 4 are left
    pop
    display -- top item gone, 3 are left
    pop
    display -- top item gone, 2 are left
    pop
    display -- top item gone, 1 is left
    pop
    display -- "Stack is empty"
    pop -- "Stack is empty"
    display -- "Stack is empty"
    push
    display -- 1 item, the top item, is the only one present
    pop
    display -- "Stack is empty"
    In the meantime, I found two problems:

    1. Input processing is not robust. The input information requested needs to be integer. When I entered letters instead, it caused the program to freeze. However, this problem has nothing to do with the persistence of the stack, which was the "problem" that you claimed to exist.

    2. When I set the program up in Visual C++ 6 in order to debug what was happening in the first problem, I got a warning that the pop() uses the temp pointer before it has been initialized; ie:
    Code:
    void pop()
    {
    	struct stack *temp;
    
    	if(tos==NULL)
    	{
    		printf("Stack is empty");
    	}
    	else
    	{
    		printf("%d value deleted",temp->info);
    
    		temp=tos;
    		tos=temp->next;
    		free(temp);
    	}
    }
    While this has no effect on the integrity or persistence of the stack, it could cause a SEGFAULT depending on exactly what garbage just happens to reside in this temp.

    Please note that while that second problem is most likely the reason for the crashing you witnessed, it still has absolutely nothing to do with the stack itself, nor with the persistence nor integrity of the stack. You are still dead wrong about that.

    And I noticed in your code (which does not compile) that you use fflush(stdin);, which is also wrong. fflush() is defined for output and update streams, but not for input streams. You are recommending a technique that uses undefined behavior, which good programming practices demand must be avoided. You are yet again misinforming your audience.

    Here is my version with that uninitialized pointer problem in pop() corrected. Test it by feeding it valid inputs and report back problems accompanied by the input and command sequence that causes the problem. And you need to demonstrate that the problem being reported was due to stack corruption or other problems caused by lack of persistence of the stack; that is, after all, what you are claiming is the problem.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include <malloc.h>
    
    struct stack
    {
        int info;
        struct stack *next;
    }*tos=NULL;
    
    void push();
    void pop();
    void peep();
    void update();
    void display ();
    
    int main()
    {
        int ans;
    
    
        do
        {
            clrscr();
    
            printf("\n 1-push ");
            printf("\n 2-pop ");
            printf("\n 3-peep ");
            printf("\n 4-update ");
            printf("\n 5-display ");
    
            printf("\n Enter chocie ");
            scanf("%d",&ans);
    
            switch (ans)
            {
                case 1:
                push();
                break;
    
                case 2:
                pop();
                break;
    
                case 3:
                peep();
                break;
    
                case 4:
                update();
                break;
    
                case 5:
                display();
                break;
    
                case 6:
                return 0;
    
                default:
                printf("\n Invalid input" );
            }
    
        getch();
        }while(ans!=6);
    
        return 0;
    }
    
    
    void push()
    {
        struct stack *temp;
    
        temp=(struct stack * ) malloc (sizeof(struct stack));
    
        printf("Enter any value");
        scanf("%d",&temp->info);
    
        temp->next=tos;
        tos=temp;
    }
    
    void pop()
    {
        struct stack *temp;
    
        if(tos==NULL)
        {
            printf("Stack is empty");
        }
        else
        {
            temp=tos;
    
            printf("%d value deleted",temp->info);
    
            tos=temp->next;
            free(temp);
        }
    }
    
    
    void display()
    {
        struct stack *temp;
    
        if(tos==NULL)
        {
            printf("\n Stack is empty ");
        }
        else
        {
            temp=tos;
    
            while(temp!=NULL)
            {
                printf("\n values are = %d", temp->info);
    
                temp=temp->next;
            }
        }
    }
    
    
    void peep()
    {
        struct stack *temp;
    
        int i=1,flag=0,n;
    
        if(tos==NULL)
        {
            printf("\n Sorry stack is empty ");
        }
        else
        {
            printf("Enter location number to view: ");
            scanf("%d",&n);
    
            temp=tos;
    
            while(temp!=NULL)
            {
                if(i==n)
                {
                    printf("%d value found at %d position", temp->info, n);
                    flag++;
                }
    
                i++;
                temp=temp->next;
            }
        }
    
    
        if(flag==0)
        {
            printf("Wrong input");
        }
    }
    
    
    void update()
    {
        struct stack *temp;
    
        int i=1,flag=0,n;
    
        if(tos==NULL)
        {
            printf("\n Sorry stack is empty ");
        }
        else
        {
            printf("Enter location number to edit : ");
            scanf("%d",&n);
    
            temp=tos;
    
            while(temp!=NULL)
            {
                if(i==n)
                {
                    printf("Enter new value" );
                    scanf("%d",&temp->info);
                    flag++;
                }
    
                i++;
                temp=temp->next;
            }
        }
    
    
        if(flag==0)
        {
            printf("Wrong input");
        }
    }
    You want to continue to claim that typedef'ing the struct is required to make the stack work? You need to support that claim with evidence. Leave your ego at the portal; there's no place for ego when working with computers.
    Last edited by dwise1_aol; February 26th, 2013 at 02:24 PM.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo