title: Memory allocation function based on chunks
body: <p>I have to create a memory allocation program that is supposed to allocate memory blocks to a memory that contains 8 contiguous memory positions. N is the size of the memory, M is the maximum number of positions a program can request and X is the number of memory positions requested. The memory is divided in M chunks, each equally made of N/M memory positions. N/M must be greater than M, otherwise a program will not have enough memory allocated to a chunk. The first element of POS stores the position (in the memory) where the first available block of 1 memory position is located, the second element stores the position (in memory) where the first available block of 2 memory positions is located and so on. If the ith element in the array POS stores number -1, it means that there are no blocks of (i+1) memory positions available in the ith chunk. If POS[X-1] stores a value different from -1, it means there is space to allocate a block of X. In that case, the value stored in POS[X-1] is returned. Otherwise, the value -1 is returned.</p>
  <p>From what I understand of the question, it wants me to create a memory allocation program that separates 8 memory positions into chunks. Each chunk is required to be about as big as M, which is the largest number of memory positions a program can request. So M is 3 in the question so each chunk should be about 3 memory positions, meaning two chunks of 3 and one chunk of 2. Now the first chunk is allocated only for requests that only need 1 memory position, second chunk for requests that need 2 contiguous memory positions and third chunk for requests that need 3 memory positions. <a href="https://imgur.com/a/D5pd0Pf" rel="nofollow noreferrer">Example using 16 memory positions</a>. Here's what I have so far.</p>
  <pre><code>#include &lt;iostream&gt; 
  using namespace std; 
    
  int search(int arr[], int n, int m, int x) 
  { 
      int POS[m];
      
      if(POS[x-1]!=-1){
          return POS[x-1];
      } return -1;
  } 
    
  int main(void) 
  { 
      int arr[] = { -1, -1, -1, -1, -1, -1, -1, -1 }; //example array, -1 represents available memory space
      int x = 2; //example number of memory positions requested 
      int n = sizeof(arr) /sizeof(arr[0]); //size of array
      int m = 3; //maximum number of positions a program can request
      
      int result = search(arr, n, m, x); 
      if(result == -1) {
          cout &lt;&lt; &quot;No slots available&quot;; 
      } else { 
          cout &lt;&lt; x &lt;&lt; &quot; slots available at index &quot; &lt;&lt; result;; 
      }
  }
  </code></pre>