Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Primative_List {
- private:
- protected:
- int GetEntry(int index);
- int list_ptr;
- public:
- Primative_List();
- virtual ~Primative_List();
- int Count();
- void Add(int object_ptr);
- void Delete();
- int Get(int index);
- };
- #include "pch.h"
- #include "Primative_List.h"
- #include <iostream>
- int Primative_List::GetEntry(int index) {
- // TODO: FROG CODE THAT EVALUATES POINTER MORE THAN ONCE (more than one set of
- // brackets)
- // Points to head of list (this->list_ptr)
- int current = this->list_ptr;
- //Iterates to the element at index 'index'
- for (int i = 0; i < index; ++i) {
- // Current points to the current element in the list, the next element is at the address stored in the 'current' variable
- // the address at 'current' contains an address to the next element in the list which contains an address to the next element in the list
- current = *(int *)current;
- }
- // Return the next element in the list
- return *(int *)current;
- }
- int Primative_List::Count()
- {
- // points to the second element in the head of the list (stores the number of elements in the list)
- return ((int *)this->list_ptr)[1];
- }
- Primative_List::Primative_List() {
- // allocates enough memory for 2 integers (sizeof(int) * 2)
- this->list_ptr = (int)malloc(sizeof(int) * 2);
- //sets the first node in the list to this->list_ptr
- *((int *)this->list_ptr) = this->list_ptr;
- //sets the second element of the head of the list to 0 (the number of elements in the list)
- ((int *)this->list_ptr)[1] = 0;
- }
- void Primative_List::Add(int object_ptr) {
- // allocates enough memory for 2 integers for a new entry in the list
- int new_entry = (int)malloc(sizeof(int) * 2);
- // sets the first element in the new_entry list element to the address at the head of the list
- *(int *)new_entry = *(int*)this->list_ptr;
- ((int *)new_entry)[1] = object_ptr;
- *(int *)this->list_ptr = new_entry;
- // increments the counter at the second node of the head of the list (this->list_ptr) array (contains the number of objects in the list)
- ++((int *)this->list_ptr)[1];
- }
- void Primative_List::Delete() {
- std::cout << "Primative_List->Delete() called.\n";
- // TODO: FROG CODE DELETE ALL AT SAME TIME (more than one set of bracket )
- // point the variable 'current' to the address at this->list_ptr
- int current = *(int*)this->list_ptr;
- for (int i = 0; i < this->Count(); ++i) {
- int old = current;
- current = *(int *)old; // deobfuscated
- free((int *)old);
- }
- free((int *)list_ptr);
- }
- int Primative_List::Get(int index) {
- // Gets the list entry at index 'index' and returns the object it points to
- return ((int *)this->GetEntry(index))[1];
- }
- Primative_List::~Primative_List() {
- std::cout << "Primative_list dtor called.\n";
- //Calls the function that deletes all the list elements in the list when user attempts to delete the Primative_List object
- this->Delete();
- }
- #include "../Primative_List/Primative_List.h"
- class String_List : public Primative_List {
- public:
- String_List();
- virtual ~String_List();
- void Delete();
- };
- #include "String_List.h"
- #include <iostream>
- String_List::String_List() : Primative_List() { }
- String_List::~String_List()
- {
- this->Delete();
- }
- void String_List::Delete()
- {
- std::cout << "String_List->Delete() called.\n";
- // TODO: FROG CODE DELETE ALL AT SAME TIME (more than one set of bracket )
- // point the variable 'current' to the address at this->list_ptr
- int current = *(int*)this->list_ptr;
- for (int i = 0; i < this->Count(); ++i) {
- free((int*)*((int*)(current)+1));
- current = *(int*)current;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement