Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include "Defines.h"
- ErrorCode executeCommand(const std::string& command, std::vector<int*>& memory)
- {
- ErrorCode code = EXECUTE_SUCCESS;
- static size_t buzyMemory = 0;
- std::istringstream istr(command);
- std::string com;
- int index;
- static bool isAllocate = false;
- istr >> com;
- if (com != "Idle")
- {
- istr >> index;
- if (index >= memory.size())
- code = INDEX_OUT_OF_BOUND;
- else if (com == "Allocate")
- {
- if (!isAllocate)
- {
- buzyMemory += index;
- isAllocate = true;
- }
- else
- code = MEMORY_LEAK;
- }
- else if (com == "Deallocate")
- {
- if (isAllocate && index == buzyMemory)
- {
- buzyMemory = 0;
- isAllocate = false;
- }
- else
- code = DOUBLE_FREE;
- }
- }
- else
- code = EXECUTE_IDLE;
- return code;
- }
- void printResult(const ErrorCode errorCode, const std::string& command)
- {
- std::cout << command << " - ";
- switch (errorCode)
- {
- case EXECUTE_SUCCESS:
- std::cout << "success" << std::endl;
- break;
- case EXECUTE_IDLE:
- std::cout << "this exam is a piece of cake! Where is the OOP already?!?" << std::endl;
- break;
- case MEMORY_LEAK:
- std::cout << "memory leak prevented, will not make allocation" << std::endl;
- break;
- case DOUBLE_FREE:
- std::cout << "system crash prevented, will skip this deallocation" << std::endl;
- break;
- case INDEX_OUT_OF_BOUND:
- std::cout << "out of bound" << std::endl;
- break;
- default:
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment