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. Advertisements

  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. Advertisements

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:
    551
    rajatag
    Feb 20, 2007
  2. Surinder Singh
    Replies:
    1
    Views:
    1,449
    Richard Bos
    Dec 20, 2007
  3. Timothy Madden
    Replies:
    1
    Views:
    2,130
    Timothy Madden
    Sep 17, 2009
  4. Casey Hawthorne
    Replies:
    3
    Views:
    1,393
    Flash Gordon
    Nov 1, 2009
  5. Debajit Adhikary
    Replies:
    36
    Views:
    3,118
    Andre Kaufmann
    Feb 10, 2011
  6. Sam Roberts
    Replies:
    1
    Views:
    362
    Yukihiro Matsumoto
    Feb 11, 2005
  7. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    524
    Kenneth McDonald
    Sep 1, 2007
  8. henq
    Replies:
    1
    Views:
    253
    Peter Scott
    Jan 2, 2004
Loading...