Скачиваний:
13
Добавлен:
01.05.2014
Размер:
3.72 Кб
Скачать
#include <stdio.h>
//#include "malloc.h"

int main(void)
{

  {
  int *p1 = (int *)malloc(1*sizeof(int));
  int *p2 = (int *)malloc(2*sizeof(int));
  int *p3 = (int *)malloc(3*sizeof(int));
  int *p4 = (int *)malloc(4*sizeof(int));
  int *p5 = (int *)malloc(5*sizeof(int)); 
  // just to know the right bound
  int *p6 = (int *)malloc(1*sizeof(int)); 
  
  *p1 = 4; 
  *p2 = -3; 
  *p3 = 12; 
  *p4 = 0; 
  *p5 = 6;     

  printf("\nTrying to allocate some memory: \n");
  printf("address - %d,  value - %d\n", p1, *p1);
  printf("address - %d,  value - %d\n", p2, *p2);  
  printf("address - %d,  value - %d\n", p3, *p3);
  printf("address - %d,  value - %d\n", p4, *p4);
  printf("address - %d,  value - %d\n", p5, *p5);
  printf("address - %d,  value - %d\n", p6, *p6);  
      
  free(p1);
  free(p2);
  free(p3);
  free(p4);  
  free(p5);
  free(p6);
  
  //sleep(2); 
   
  printf("\nThen free this memory\n");
  printf("address - %d,  value - %d\n", p1, *p1);
  printf("address - %d,  value - %d\n", p2, *p2);  
  printf("address - %d,  value - %d\n", p3, *p3);
  printf("address - %d,  value - %d\n", p4, *p4);
  printf("address - %d,  value - %d\n", p5, *p5);
  printf("address - %d,  value - %d\n", p6, *p6);  
  
  
  //sleep(2);
       
  printf("\nOK, then let's allocate memory one more time:\n"); 
  
  p1 = (int *)malloc(1*sizeof(int));
  p2 = (int *)malloc(2*sizeof(int));
  p3 = (int *)malloc(3*sizeof(int));
  p4 = (int *)malloc(4*sizeof(int));
  p5 = (int *)malloc(5*sizeof(int));  
    // just to know the right bound
  p6 = (int *)malloc(1*sizeof(int)); 
    
  printf("address - %d,  value - %d\n", p1, *p1);
  printf("address - %d,  value - %d\n", p2, *p2);  
  printf("address - %d,  value - %d\n", p3, *p3);
  printf("address - %d,  value - %d\n", p4, *p4);
  printf("address - %d,  value - %d\n", p5, *p5);
  printf("address - %d,  value - %d\n", p6, *p6);  
  
  free(p1);
  free(p2);
  free(p3);
  free(p4);  
  free(p5);
  free(p6);
  
 // sleep(2);
    
  printf("\nWell, now free the memory and then  allocate the same chuck one more time\n"); 
  printf("But this time let's allocate this blocks of memory in random order. \n"); 
  
  p1 = (int *)malloc(5*sizeof(int));
  p2 = (int *)malloc(1*sizeof(int));
  p3 = (int *)malloc(4*sizeof(int));
  p4 = (int *)malloc(3*sizeof(int));
  p5 = (int *)malloc(2*sizeof(int));  
    // just to know the right bound
  p6 = (int *)malloc(1*sizeof(int)); 
  
  printf("address - %d,  value - %d\n", p1, *p1);
  printf("address - %d,  value - %d\n", p2, *p2);  
  printf("address - %d,  value - %d\n", p3, *p3);
  printf("address - %d,  value - %d\n", p4, *p4);
  printf("address - %d,  value - %d\n", p5, *p5);
  printf("address - %d,  value - %d\n", p6, *p6);  
  
  printf("\nGood, now let's free this chuck of memory, but leave some piece of it marked as used,\n");
  printf("then let's allocate the same amount of memory and see what we'll get.\n"); 
 
 free(p1);
  free(p2);
  free(p3);
  //free(p4);   !
  free(p5);
  free(p6);
  
  p1 = (int *)malloc(1*sizeof(int));
  p2 = (int *)malloc(2*sizeof(int));
  p3 = (int *)malloc(3*sizeof(int));
  // let's make another pointer
  // coz of "using" this piece of memory, connected with this ponter
  int *p7 = (int *)malloc(4*sizeof(int));
  p5 = (int *)malloc(5*sizeof(int));  
    // just to know the right bound
  p6 = (int *)malloc(1*sizeof(int)); 
  
  printf("address - %d,  value - %d\n", p1, *p1);
  printf("address - %d,  value - %d\n", p2, *p2);  
  printf("address - %d,  value - %d\n", p3, *p3);
  printf("address - %d,  value - %d\n", p7, *p7);
  printf("address - %d,  value - %d\n", p5, *p5);
  printf("address - %d,  value - %d\n", p6, *p6);  
  
  //sleep(2);  
  }
  
  return 0;
}
Соседние файлы в папке Лабораторная работа №36