Thread: raising errors

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    69

    raising errors

    Suppose I have the following in a pushdown stack implementation:

    Code:
    static int *stack;
    static unsigned int max_size
    static unsigned int item_count;
    
    void init(unsigned int size) {
    
      max_size = size;
      stack = malloc(size * sizeof(int));
      item_count = 0;
    
    }
    
    void push(int value) {
      
      if (item_count < max_size) {
        stack[item_count++] = value;
      } else {
        // raise error -- stack is full. how do i do this?
      }
    
    }
    What is the proper way of raising/handling errors if the stack has already reached its maximum?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends somewhat on the specification you're writing your program to. Is your stack supposed to grow as necessary? If so, do that. Otherwise, I'd imagine you'd want to return a status code (typically 0 for success, and some other number for not-success).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It sort of depends on the use - you could have a "success/fail" return value (e.g. return 0 for succes, -1 for fail, or 1 for success, 0 for fail, depending on your "taste" in return values). This approach moves the problem on to the next level up to check for the error and know what to do. Make sure you use a consistent method for returning "ok" vs. "failed", so that you don't have to "remember" which function returns negative for fail, and which returns 0 for fail and which returns a positive error code for fail... Consistency is important here.

    Alternatively, you just "kill the app" by something like
    Code:
    fprintf(stderr, "Error, stack overflow, got %d elements, max is %d elements", item_count, max_size+1);
    exit(1);
    In this case, the calling code has no choice in the matter, and no say in how to deal with the error.

    A third option would be to realloc the stack array (until that fails, and then panic) - which is OK if you have other checks to detect for example infinitely recursive calls to push() - otherwise you may be waiting quite some time before the stack fills the entire machines memory....

    To do appropriate error handling method is very dependant on what you are using the functions for, and what the likelyhood of failing is - if you have a situation where you DON'T EVER expect the stack to run out, and if it does, something else is most likely very wrong anyways, exiting is probably OK. If, on the other hand, you are dealing with user-input that may not be saved by the time your push function is called, and it's entirely possible (if not entirely LIKELY or correct) that the user inputs some expression that leads to the stack overflowing, the second approach should most likely be applied.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM