Advertisement
jkavart

Untitled

Feb 9th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. /* Objective: Exhaust the memory to understand fragmentation and explain the timings.
  6. *
  7. */
  8.  
  9. int main(int argc, char** argv)
  10. {
  11.     double t, t2;
  12.    
  13.         double dif, dif2;
  14.         const int m = 100; //Change this
  15.        
  16.         printf("Step One\n"); //DO NOT CHANGE
  17.         int **container= new int*[3 * m];
  18.         //int **container2=new int*[m];
  19.         t = clock();
  20.         for(int i = 0; i < (3 * m); i++)
  21.         {
  22.             container[i]=new int[800000];
  23.         }
  24.         t = clock()-t;
  25.         t2=clock();
  26.         printf("Step Two\n"); //DO NOT CHANGE
  27.         for(int i = 0; i < (3 * m); i += 2)
  28.         {
  29.             delete[] container[i];
  30.             //container[i]=NULL;
  31.         }
  32.    
  33.    
  34.         printf("Step Three\n\n"); //DO NOT CHANGE
  35.        
  36.         for(int i = 0; i <(3*m); i +=2)
  37.         {
  38.             container[i]=new int[900000];
  39.         }
  40.         t2=clock()-t2;
  41.        
  42.        
  43.    
  44.     printf("First array allocation: %.5f\n", t*1000000);
  45.     printf("Second array allocation: %.5f\n", t2*1000000);
  46.     system("pause");
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement