c++ - What are any side-effects of declaring something as size_t that I should be aware of? -
making class work stack (code not mine)
i have code this:
class stack { private: int size; int* data; // next line mean? size_t ptr; public: stack(int valid_stack_size) { this->size = valid_stack_size; this->ptr = 0; this->data = new int[valid_stack_size]; } void push(int value) { if (this->ptr >= (size_t)this->size) cout << "stack full" << endl; this->data[this->ptr++] = value; } int pop() { if (this->ptr == 0) cout << "stack empty" << endl; return this->data[--this->ptr]; } };
what size_t ptr mean? size_t takes ptr value? or?
size_t
type - defined in header file cstddef
type can hold maximum size of object. on modern systems, it's equal size of pointer, , 64 bit (8 bytes). however, not required standard.
as result, line declares variable ptr
of type size_t
, confusing - ptr
name suggest variable type should pointer.
on side note, code exhibits undefined behavior - when popping on empty stack or pushing full one.
Comments
Post a Comment