Created
October 15, 2015 15:02
-
-
Save krysseltillada/7347198369089f95a2c1 to your computer and use it in GitHub Desktop.
memory handling (vectors)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int main () | |
{ | |
int sz = 0, unalloc_space = 1; | |
int *elem, input = 0, *alloc_mem = nullptr, *temp_mem; | |
while (std::cin >> input) { | |
if (alloc_mem == nullptr) { | |
++unalloc_space; | |
unalloc_space *= unalloc_space; | |
alloc_mem = new int [unalloc_space]; | |
std::cout << "unalloc space " << unalloc_space << std::endl; | |
} | |
if (sz == unalloc_space) { | |
std::cout << "allocating resources " << sz << std::endl; | |
unalloc_space *= unalloc_space; | |
temp_mem = new int [unalloc_space]; | |
for (int i = 0; i != sz; ++i) | |
*(temp_mem + i) = *(alloc_mem + i); | |
delete alloc_mem; | |
alloc_mem = new int [unalloc_space]; | |
for (int i = 0; i != sz; ++i) | |
*(alloc_mem + i) = *(temp_mem + i); | |
delete temp_mem; | |
} | |
*(alloc_mem + sz) = input; | |
++sz; | |
} | |
for (int i = 0; i != sz; ++i) | |
std::cout << *(alloc_mem + i) << std::endl; | |
delete alloc_mem; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment