Help debugging stack program

Discussion in 'C Programming' started by White Spirit, Feb 6, 2006.

  1. White Spirit

    White Spirit Guest

    The following program (code fragment but is supposed to work) which
    implements three stacks segfaults when run and I can't for the life of
    me think why. It worked perfectly before I changed it to use pointers,
    and it doesn't get as far as printing the string in main.

    #include <malloc.h>
    #include <stdio.h>
    #include <stdlib.h>

    #define SIZEOFSTACK 100

    struct stack
    {
    char value[SIZEOFSTACK];
    int size;
    } *controlStack, *resultStack, *memoryStack;

    void pushToStack (char v, struct stack *ptrStack)
    {
    if (ptrStack->size < SIZEOFSTACK)
    {
    ptrStack->value[ptrStack->size] = v;
    ptrStack->size++;
    }

    else

    {
    printf ("Error - stack out of bounds.\n");
    }
    }

    char popFromStack (struct stack *ptrStack)
    {
    if (ptrStack->size == 0)
    {
    printf("Error - attempt to pop a value from an empty stack.");
    /* Exception handling will go here */
    }

    else
    {
    return ptrStack->value[--ptrStack->size];
    }
    }

    int main ()
    { printf ("got this far");
    pushToStack('5', resultStack);
    pushToStack('6', resultStack);
    pushToStack('1', resultStack);
    pushToStack('2', resultStack);

    int i;

    for (i = 1; i <=4; i++)
    {
    printf ("Popped %c\n", popFromStack(resultStack));
    }
    return (EXIT_SUCCESS);
    }
    White Spirit, Feb 6, 2006
    #1
    1. Advertising

  2. In article <43e7db50$>,
    White Spirit <> wrote:
    >The following program (code fragment but is supposed to work) which
    >implements three stacks segfaults when run and I can't for the life of
    >me think why. It worked perfectly before I changed it to use pointers,
    >and it doesn't get as far as printing the string in main.


    >#define SIZEOFSTACK 100
    >
    >struct stack
    >{
    > char value[SIZEOFSTACK];
    > int size;
    >} *controlStack, *resultStack, *memoryStack;


    Okay, non-automatic variables, so they will be initialized to 0.
    So controlStack and resultStack and memoryStack start out as
    NULL pointers.

    The problem should be obvious now ;-)
    --
    "law -- it's a commodity"
    -- Andrew Ryan (The Globe and Mail, 2005/11/26)
    Walter Roberson, Feb 6, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rajatag
    Replies:
    4
    Views:
    407
    rajatag
    Feb 20, 2007
  2. Surinder Singh
    Replies:
    1
    Views:
    1,195
    Richard Bos
    Dec 20, 2007
  3. Casey Hawthorne
    Replies:
    3
    Views:
    1,085
    Flash Gordon
    Nov 1, 2009
  4. Debajit Adhikary
    Replies:
    36
    Views:
    2,289
    Andre Kaufmann
    Feb 10, 2011
  5. Sam Roberts
    Replies:
    1
    Views:
    224
    Yukihiro Matsumoto
    Feb 11, 2005
Loading...

Share This Page