/* generic type pushed into the stack */
/* in this case it's just an int */
typedef int item_t;
/* our stack data type */
typedef struct {
int size;
int capacity;
int *elements; // problem here
} stack;
void* pop_stack(stack *mystack); // same here
If your stack is supposed to hold item_t
s, then it should hold item_t
s, not ints, and the pop
function should return item_t*
.
stack* create_stack(int capacity)
{
stack *stack_ptr = (stack*) malloc(sizeof(stack));
if(stack_ptr == NULL)
handle_error("Error while allocating memory.");
int *elements_ptr = (item_t *) malloc(capacity*sizeof(item_t));
if(elements_ptr == NULL)
handle_error("Error while allocating memory.");
If the second malloc failed, you leaked stack_ptr
. Not a real big problem since you exit
right after it, but worth correcting anyway.
Also please read Do I cast the result of malloc? Do I cast the result of malloc? (Spoiler: no you should not.) Plus you've got stack*
and item_t *
. Your spacing should be consistent - pick one or the other.
void delete_stack(stack *mystack)
{
free(mystack->elements);
free(mystack);
mystack = NULL; // <- no-op
}
That last assignment is useless - it only modifies the local variable that is not used further. Remove it to avoid confusion. If you meant to zero-out the callers pointer, you need to take that parameter with a pointer-to-pointer.
/* generic type pushed into the stack */
/* in this case it's just an int */
typedef int item_t;
/* our stack data type */
typedef struct {
int size;
int capacity;
int *elements; // problem here
} stack;
void* pop_stack(stack *mystack); // same here
If your stack is supposed to hold item_t
s, then it should hold item_t
s, not ints, and the pop
function should return item_t*
.
stack* create_stack(int capacity)
{
stack *stack_ptr = (stack*) malloc(sizeof(stack));
if(stack_ptr == NULL)
handle_error("Error while allocating memory.");
int *elements_ptr = (item_t *) malloc(capacity*sizeof(item_t));
if(elements_ptr == NULL)
handle_error("Error while allocating memory.");
If the second malloc failed, you leaked stack_ptr
. Not a real big problem since you exit
right after it, but worth correcting anyway.
Also please read Do I cast the result of malloc? (Spoiler: no you should not.) Plus you've got stack*
and item_t *
. Your spacing should be consistent - pick one or the other.
void delete_stack(stack *mystack)
{
free(mystack->elements);
free(mystack);
mystack = NULL; // <- no-op
}
That last assignment is useless - it only modifies the local variable that is not used further. Remove it to avoid confusion. If you meant to zero-out the callers pointer, you need to take that parameter with a pointer-to-pointer.
/* generic type pushed into the stack */
/* in this case it's just an int */
typedef int item_t;
/* our stack data type */
typedef struct {
int size;
int capacity;
int *elements; // problem here
} stack;
void* pop_stack(stack *mystack); // same here
If your stack is supposed to hold item_t
s, then it should hold item_t
s, not ints, and the pop
function should return item_t*
.
stack* create_stack(int capacity)
{
stack *stack_ptr = (stack*) malloc(sizeof(stack));
if(stack_ptr == NULL)
handle_error("Error while allocating memory.");
int *elements_ptr = (item_t *) malloc(capacity*sizeof(item_t));
if(elements_ptr == NULL)
handle_error("Error while allocating memory.");
If the second malloc failed, you leaked stack_ptr
. Not a real big problem since you exit
right after it, but worth correcting anyway.
Also please read Do I cast the result of malloc? (Spoiler: no you should not.) Plus you've got stack*
and item_t *
. Your spacing should be consistent - pick one or the other.
void delete_stack(stack *mystack)
{
free(mystack->elements);
free(mystack);
mystack = NULL; // <- no-op
}
That last assignment is useless - it only modifies the local variable that is not used further. Remove it to avoid confusion. If you meant to zero-out the callers pointer, you need to take that parameter with a pointer-to-pointer.
/* generic type pushed into the stack */
/* in this case it's just an int */
typedef int item_t;
/* our stack data type */
typedef struct {
int size;
int capacity;
int *elements; // problem here
} stack;
void* pop_stack(stack *mystack); // same here
If your stack is supposed to hold item_t
s, then it should hold item_t
s, not ints, and the pop
function should return item_t*
.
stack* create_stack(int capacity)
{
stack *stack_ptr = (stack*) malloc(sizeof(stack));
if(stack_ptr == NULL)
handle_error("Error while allocating memory.");
int *elements_ptr = (item_t *) malloc(capacity*sizeof(item_t));
if(elements_ptr == NULL)
handle_error("Error while allocating memory.");
If the second malloc failed, you leaked stack_ptr
. Not a real big problem since you exit
right after it, but worth correcting anyway.
Also please read Do I cast the result of malloc? (Spoiler: no you should not.) Plus you've got stack*
and item_t *
. Your spacing should be consistent - pick one or the other.
void delete_stack(stack *mystack)
{
free(mystack->elements);
free(mystack);
mystack = NULL; // <- no-op
}
That last assignment is useless - it only modifies the local variable that is not used further. Remove it to avoid confusion. If you meant to zero-out the callers pointer, you need to take that parameter with a pointer-to-pointer.