Advertisement
imk0tter

C++ List stuff

May 11th, 2023
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. class Primative_List {
  2. private:
  3. protected:
  4.   int GetEntry(int index);
  5.   int list_ptr;
  6. public:
  7.   Primative_List();
  8.   virtual ~Primative_List();
  9.  
  10.   int Count();
  11.  
  12.   void Add(int object_ptr);
  13.  
  14.   void Delete();
  15.  
  16.   int Get(int index);
  17. };
  18.  
  19. #include "pch.h"
  20.  
  21. #include "Primative_List.h"
  22. #include <iostream>
  23.  
  24. int Primative_List::GetEntry(int index) {
  25.   // TODO: FROG CODE THAT EVALUATES POINTER MORE THAN ONCE (more than one set of
  26.   // brackets)
  27.  
  28.   // Points to head of list (this->list_ptr)
  29.   int current = this->list_ptr;
  30.  
  31.   //Iterates to the element at index 'index'
  32.   for (int i = 0; i < index; ++i) {
  33.  
  34.     // Current points to the current element in the list, the next element is at the address stored in the 'current' variable
  35.     // 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
  36.     current = *(int *)current;
  37.   }
  38.   // Return the next element in the list
  39.   return *(int *)current;
  40. }
  41.  
  42. int Primative_List::Count()
  43. {
  44.     // points to the second element in the head of the list (stores the number of elements in the list)
  45.     return ((int *)this->list_ptr)[1];
  46. }
  47.  
  48. Primative_List::Primative_List() {
  49.   // allocates enough memory for 2 integers (sizeof(int) * 2)
  50.   this->list_ptr = (int)malloc(sizeof(int) * 2);
  51.  
  52.   //sets the first node in the list to this->list_ptr
  53.   *((int *)this->list_ptr) = this->list_ptr;
  54.  
  55.   //sets the second element of the head of the list to 0 (the number of elements in the list)
  56.   ((int *)this->list_ptr)[1] = 0;
  57. }
  58.  
  59. void Primative_List::Add(int object_ptr) {
  60.   // allocates enough memory for 2 integers for a new entry in the list
  61.   int new_entry = (int)malloc(sizeof(int) * 2);
  62.  
  63.   // sets the first element in the new_entry list element to the address at the head of the list
  64.   *(int *)new_entry = *(int*)this->list_ptr;
  65.   ((int *)new_entry)[1] = object_ptr;
  66.   *(int *)this->list_ptr = new_entry;
  67.   // 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)
  68.   ++((int *)this->list_ptr)[1];
  69. }
  70.  
  71. void Primative_List::Delete() {
  72.     std::cout << "Primative_List->Delete() called.\n";
  73.   // TODO: FROG CODE DELETE ALL AT SAME TIME (more than one set of bracket )
  74.  
  75.     // point the variable 'current' to the address at this->list_ptr
  76.   int current = *(int*)this->list_ptr;
  77.   for (int i = 0; i < this->Count(); ++i) {
  78.     int old = current;
  79.     current = *(int *)old; // deobfuscated
  80.     free((int *)old);
  81.   }
  82.   free((int *)list_ptr);
  83. }
  84.  
  85. int Primative_List::Get(int index) {
  86.   // Gets the list entry at index 'index' and returns the object it points to
  87.   return ((int *)this->GetEntry(index))[1];
  88. }
  89.  
  90. Primative_List::~Primative_List() {
  91.     std::cout << "Primative_list dtor called.\n";
  92.     //Calls the function that deletes all the list elements in the list when user attempts to delete the Primative_List object
  93.     this->Delete();
  94. }
  95.  
  96. #include "../Primative_List/Primative_List.h"
  97.  
  98. class String_List : public Primative_List {
  99. public:
  100.     String_List();
  101.     virtual ~String_List();
  102.     void Delete();
  103. };
  104.  
  105. #include "String_List.h"
  106. #include <iostream>
  107.  
  108. String_List::String_List() : Primative_List() { }
  109. String_List::~String_List()
  110. {
  111.     this->Delete();
  112. }
  113. void String_List::Delete()
  114. {
  115.     std::cout << "String_List->Delete() called.\n";
  116.   // TODO: FROG CODE DELETE ALL AT SAME TIME (more than one set of bracket )
  117.   // point the variable 'current' to the address at this->list_ptr
  118.     int current = *(int*)this->list_ptr;
  119.     for (int i = 0; i < this->Count(); ++i) {
  120.         free((int*)*((int*)(current)+1));
  121.         current = *(int*)current;
  122.     }
  123. }
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement