diff options
author | Nao Pross <naopross@thearcway.org> | 2018-12-08 12:58:04 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-12-08 12:58:04 +0100 |
commit | 05f2df34290af477b0fee49b75e5f56e1d6c83f9 (patch) | |
tree | 66a7214321c874be02d1a4a9b4fd3f489dc09ad8 /memory-classes.cpp | |
download | cplusplus-05f2df34290af477b0fee49b75e5f56e1d6c83f9.tar.gz cplusplus-05f2df34290af477b0fee49b75e5f56e1d6c83f9.zip |
Initial commit with kinda crappy unnumbered examples
Diffstat (limited to '')
-rw-r--r-- | memory-classes.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/memory-classes.cpp b/memory-classes.cpp new file mode 100644 index 0000000..19d17e9 --- /dev/null +++ b/memory-classes.cpp @@ -0,0 +1,53 @@ +#include "memory-classes.hpp" + +#include <iostream> + + +lifo::lifo(std::size_t max_size_) : max_size(max_size_) { + m_storage = new int[max_size]; +} + +lifo::~lifo() { + if (m_storage != nullptr) + delete m_storage; +} + +void lifo::push(int v) { + if (m_top >= max_size) + return; + + m_storage[m_top++] = v; +} + +int lifo::pop() { + if (m_top <= 0) + return 0; + + return m_storage[--m_top]; +} + + +int main(int argc, char *argv[]) { + + // the lifo allocates a memory buffer + lifo stack(5); + + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + stack.push(5); + + std::cout << stack.pop() << std::endl; + std::cout << stack.pop() << std::endl; + std::cout << stack.pop() << std::endl; + std::cout << stack.pop() << std::endl; + std::cout << stack.pop() << std::endl; + + return 0; + + // here, when the scope ends the stack object is destroyed + // as it goes *out of scope*, and so the destructor is called + // which frees the memory + +} |