Generally, the type of the stack element is void* or, somewhat better, uint64_t. Then you can put any type you like in the stack. If sizeof(T) <= sizeof(uint64_t), then just cast the element types; otherwise dynamically allocated the Ts and store the pointers in the elements.
Also, most implementations aren't of fixed size; instead, each element has a next pointer. Lastly, if you use a singly linked list for the implementation, if you do just a bit more work, make it support full list operations and you get stack operations for free since a stack is a subset of a list.
The reason for using uint64_t is because it's guaranteed to work on all platforms, specifically 32-bit platforms where sizeof(void*) is 4 — uint64_tguarantees 8.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Generally, the type of the stack element is
void*
or, somewhat better,uint64_t
. Then you can put any type you like in the stack. Ifsizeof(T)
<=sizeof(uint64_t)
, then just cast the element types; otherwise dynamically allocated theT
s and store the pointers in the elements.Also, most implementations aren't of fixed size; instead, each element has a
next
pointer. Lastly, if you use a singly linked list for the implementation, if you do just a bit more work, make it support full list operations and you get stack operations for free since a stack is a subset of a list.Thanks for the feedback, I already used
void*
but neveruint64_t
I will research about it.The reason for using
uint64_t
is because it's guaranteed to work on all platforms, specifically 32-bit platforms wheresizeof(void*)
is 4 —uint64_t
guarantees 8.