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.