Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторная работа №36 / Лаба 3 по осям.doc
Скачиваний:
15
Добавлен:
01.05.2014
Размер:
53.76 Кб
Скачать

3. Разработка тестового примера

#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 = 1;

*p2 = -2;

*p3 = 3;

*p4 = 4;

*p5 = 5;

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);

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);

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, \n", p1);

printf("address - %d, \n", p2);

printf("address - %d, \n", p3);

printf("address - %d, \n", p4);

printf("address - %d, \n", p5);

printf("address - %d, \n", p6);

free(p1);

free(p2);

free(p3);

free(p4);

free(p5);

free(p6);

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);

}

return 0;

}

Соседние файлы в папке Лабораторная работа №36