Thread: Stack Implementation and Exceptions

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Stack Implementation and Exceptions

    I have this code:

    Code:
    typedef struct
    {
       int *data;
    
       size_t initSize; //initial size
       size_t multSize; //size multiplier
    
       size_t pointer;  //current pointer
    
    } Stack, *PStack;
    
    int createStack(PStack stack, size_t size)
    {
    
       (*stack).data = NULL;
    
       (*stack).initSize = size;
       (*stack).multSize = 0;
    
       (*stack).pointer = 0;
    
       //allocate stack for the first time
       return enlargeStack(&(*stack));
    }
    
    int enlargeStack(PStack stack)
    {
       (*stack).multSize++;
    
    
       return ((*stack).data = realloc((*stack).data,
             (*stack).initSize * (*stack).multSize * sizeof(int))) != NULL;
    }
    
    int pushStack(PStack stack, int data)
    {
       if((*stack).pointer >= ((*stack).initSize * (*stack).multSize))
          if(!enlargeStack(&(*stack)))
             return 0;
    
       (*stack).data[(*stack).pointer] = data;
    
       (*stack).pointer++;
    
       return 1;
    }
    
    int peekStack(const PStack stack, int *buffer)
    {
       if((*stack).pointer == 0)
          return 0;
    
       *buffer = (*stack).data[(*stack).pointer - 1];
    
       return 1;
    }
    
    int popStack(PStack stack, int *buffer)
    {
       if((*stack).pointer == 0)
          return 0;
    
       int temp;
    
       if(!peekStack(&(*stack), &temp))
          return 0;
    
       *buffer = temp;
    
       (*stack).pointer--;
    
       return 1;
    }
    
    void freeStack(PStack stack)
    {
       free((*stack).data);
    
       (*stack).data = NULL;
    
       (*stack).initSize = 0;
       (*stack).multSize = 0;
    
       (*stack).pointer = 0;
    }
    And...

    Code:
    int main()
    {
       Stack stack;
    
       if(!createStack(&stack, 10))
          exit(EXIT_FAILURE);
    
       int buffer;
    
       if(popStack(&stack, &buffer)) //won't work
          printf("pop: %d\n", buffer);
    
       if(peekStack(&stack, &buffer)) //won't work
          printf("peek: %d\n", buffer);
    
       pushStack(&stack, 2);
       pushStack(&stack, 3);
    
       if(peekStack(&stack, &buffer)) //works
          printf("peek: %d\n", buffer);
    
       if(popStack(&stack, &buffer)) //works
          printf("pop: %d\n", buffer);
    
       if(popStack(&stack, &buffer)) //works
          printf("pop: %d\n", buffer);
    
       if(popStack(&stack, &buffer)) //won't work
          printf("pop: %d\n", buffer);
    
       int i;
    
       for(i=0; i<100; i++)
       {
          pushStack(&stack, i);
       }
    
       for(i=0; i<100; i++)
       {
          if(popStack(&stack, &buffer)) //won't work
             printf("pop[%d]: %d\n",i, buffer);
       }
    
       if(popStack(&stack, &buffer)) //still won't work :)
          printf("pop: %d\n", buffer);
    
       freeStack(&stack);
    
       return 0;
    }
    Is there an easy way to handling exceptions in C? So we don't need to returning int from functions like the APIs...
    Last edited by audinue; 06-21-2008 at 12:33 PM. Reason: bad english :P

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, look at this
    http://www.on-time.com/ddj0011.htm
    Generally google, read and you will find some libraries with exception handling in C

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Allow me to introduce you to the -> (arrow) operator.
    These two are the same:
    Code:
       (*stack).data = NULL;
    and
    Code:
       stack->data = NULL;
    Also:
    Code:
    (&(*stack)
    is the same as just
    Code:
    stack
    Isn't that tidier?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thanks iMalc You are very helpful!

    Btw, I begin to confuse when trying to implement stack of string >_<"

    Any idea??

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, in C, string are represented as "char *". So, from what I see from your stack implementation, you want an array or string (ie, an array of char*), that's right ? So it would be
    Code:
    char **data;
    instead of
    Code:
    int *data;
    If you don't want to dynamically allocate memory for each of your strings (that's it, you want a fixed-length size for you string, which isn't really handy by the way), I don't really know how to do it, but anyway you won't see this really often, but I think it's something like

    Code:
    char (*data)[MAX_CHAR+1];   // Note that I'm not really sure about the syntax, I might be totally wrong
    where MAX_CHAR is the max number of characters each string can receive.

    EDIT: I just did some test and it looks fine.
    Last edited by foxman; 06-22-2008 at 09:40 AM.
    I hate real numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about constructors and exceptions
    By Elkvis in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2008, 06:15 PM
  2. exception handling, function call and memory
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 01-30-2008, 08:00 AM
  3. Exceptions and constructors
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2007, 11:20 AM
  4. Exceptions. (This should be easy)
    By skanxalot in forum C++ Programming
    Replies: 11
    Last Post: 03-04-2003, 10:44 PM