#Generic Stack

14 messages · Page 1 of 1 (latest)

versed cosmos
grim field
#

I really liked how you wrote this stack I will give you some thoughts i have they are only my point of view

#

The first issue is that your stack tries to manage the lifetime and semantics

#

I think a C container should be "dumb" It should only be responsible for:

#
  1. Managing a block of memory.
  2. ​Copying bytes into that memory (push).
  3. ​Copying bytes out of that memory (pop).
  4. ​Freeing that block of memory.
#

the user should be 100% responsible for what those bytes mean

#

By trying to build this logic into the stack itself, you create a leaky abstraction

#

For example, stack_release_ptr assumes the stack contains pointers. What if the user made a mistake and it contains ints? your function will try to free() an integer leading to a crash

#

I think stack_push_ptr_deepcpy logic belongs in the user's code not in the stack library

#

And stack_release_ptr is dangerous is only wokrs if s->data_type_size is exactly sizeof(void*) but if the user set data_type_size to sizeof(int) but thought they were storing pointers this function would memcpy an int's bytes into the ptr variable and then try to free(ptr), which would be a free() on a garbage address

#

Other functions are good no notes

#

A small thing i just realized is you forgot #include <stdbool.h> and you used bool

#

My point is everyone has responsibility and the user is responsible of his data lifetime

#

Anyway great piece of code