Thread: Stack Implementation No Errors

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    36

    Stack Implementation No Errors

    Hello everyone,

    I am writing some code to implement a stack in C. For this is am using this website article as a guide https://www.cs.bu.edu/teaching/c/stack/array/

    I was testing the code as I go and add the features and I noticed I did not get any error while trying to access an item from an index beyond the the allocation of the stack. For example I have a stack of 2 elements and I still can access elements at index 500. (I was expecting segmentation here)

    These are my files:

    stack.h

    Code:
    #define STACK_MIN_CAPACITY 1 /* minimum capacity for the stack */
    
    
    /*general enumerators and types. */
    #ifndef CORE_H
    #define    CORE_H
        typedef enum bool {FALSE, TRUE} Bool;
        typedef enum error {NO_ERRORS, ERROR} Error;
    #endif
    
    /* stack realated definitions */
    #ifndef STACK_H
    #define STACK_H
    
    /* type definitions */
    typedef char StackItem;
    typedef struct stack{
        StackItem * items;
        int top;
        int capacity;
    } Stack;
    
    /*function prototypes */
    Bool stackInitialize(Stack *);
    void stackTerminate(Stack *);
    Bool stackExpand(Stack *);
    Bool stackPush(Stack *, StackItem);
    StackItem stackPop(Stack *);
    StackItem stackPeek(Stack *);
    Bool stackIsEmpty(Stack *);
    
    #endif

    stack.c

    Code:
    /********************************************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include "stack.h"
    /********************************************************************/
    
    /********************************************************************
     Purpose    : Initializes a Stack
    *********************************************************************/
    Bool stackInitialize(Stack * stack){
    
        StackItem * items;
        items = (StackItem *) malloc(sizeof(StackItem) * STACK_MIN_CAPACITY);
    
        if(items == NULL){
            fprintf(stderr, "%s\n","Could not allocate memory");
            return FALSE ;
        }else{
            stack->items = items;
            stack->capacity = STACK_MIN_CAPACITY;
            stack->top = -1;
            return TRUE;
        }
    }
    
    /********************************************************************
     Purpose    : Terminates a Stack
    *********************************************************************/
    void stackTerminate(Stack * stack){
    
        free(stack->items);
    
        stack->items = NULL;
        stack->capacity = 0;
        stack->top = -1;
    }
    
    
    /********************************************************************
     Purpose    : Expands a Stack to hold more items
    *********************************************************************/
    Bool stackExpand(Stack * stack){
    
        StackItem * temp = NULL;
        int newSize = stack->capacity * 2;
    
        temp = (StackItem *) realloc(stack->items,
                                     sizeof(stack->items[0]) * newSize);
        if(temp == NULL){
            fprintf(stderr, "%s\n", "Stack could not be expanded");
            return FALSE;
        }else{
            stack->capacity = newSize;
            stack->items = temp;
            return TRUE;
        }
    }
    
    /********************************************************************
     Purpose    : Pushes element to the stack
    *********************************************************************/
    Bool stackPush(Stack * stack, StackItem item){
        Bool success;
    
        if(stack->top >= stack->capacity - 1){
            success = stackExpand(stack);
        }else{
        }
    
        if(success){
            stack->items[++stack->top] = item;
            return TRUE;
        }else{
            fprintf(stderr, "%s\n","Could not add element to stack");
            return FALSE;
        }
    }
    test.c

    Code:
    #import <stdio.h>
    #import "stack.h"
    
    int main(int argc, char const *argv[]){
    
        Stack stack;
        stackInitialize(&stack);
        
        /*Add a few items and check them */
        stackPush(&stack, 'A');
        stackPush(&stack, 'B');
    
        printf("%c\n", stack.items[0] );
        printf("%c\n", stack.items[500] ); /*expected an error as well*/
        
        return 0;
    }
    Thanks a lot in advance for the help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,154
    > printf("%c\n", stack.items[500] ); /*expected an error as well*/
    The problem is, you dive straight in and access the array directly, without going through the interface you've provided.

    In a C++ world, you would make items 'private', thus providing you with some insulation against careless programming.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    36
    Thanks a lot for the reply Salem,

    I guess C can do a lot of stuff with memory if I am not careful. Since this is a stack I will not give the users the option to access the items this was just me debugging and trying to triple check. I just need to add the procedures: stackPush(), StackPop(), stackPeek().

    Thanks a lot for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack implementation using arrays
    By Mishu Singh in forum C Programming
    Replies: 1
    Last Post: 05-31-2014, 12:36 AM
  2. Aritmethic operations on Stack (Array implementation)
    By ilerleartik in forum C Programming
    Replies: 12
    Last Post: 03-11-2012, 06:24 AM
  3. Replies: 5
    Last Post: 07-01-2011, 11:35 AM
  4. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 09:32 AM
  5. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM

Tags for this Thread