html5-img
1 / 9

Mäluhaldus v2

Mäluhaldus v2. Usalda, kuid kontrolli!. Mälu on piiratud ressurss Eelmine kord küsisime mälu, kuid mis juhtub, kui meil seda piisavalt pole? #include < stdio.h > #include < malloc.h > int main ( void ) { int * ptr1 , * ptr2 ; ptr1 = ( int *) malloc ( sizeof ( int ) * 100 );

senta
Télécharger la présentation

Mäluhaldus v2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mäluhaldus v2

  2. Usalda, kuid kontrolli! • Mälu on piiratud ressurss • Eelmine kord küsisime mälu, kuid mis juhtub, kui meil seda piisavalt pole? #include <stdio.h> #include <malloc.h> intmain(void){ int*ptr1, *ptr2; ptr1=(int*)malloc(sizeof(int)*100); ptr2=(int*)malloc(sizeof(int)*50000000000000000); printf("ptr1 adr = %p\nptr2 adr = %p", ptr1, ptr2); free(ptr1); free(ptr2); return 0; }

  3. Usalda, kuid kontrolli! • Mälu on piiratud ressurss • Eelmine kord küsisime mälu, kuid mis juhtub, kui meil seda piisavalt pole? • Vaba mälu otsitakse ühe suure vaba tükina

  4. Näide 1 – Mälu allokeerimisekontroll #include <stdio.h> #include <stdlib.h> intmain(void){ int*ptr; ptr=(int*)malloc(sizeof(int)*50000000000000000); if(!ptr) { printf("Pole piisavalt mälu"); exit(1); } return 0; }

  5. calloc(); Calloc() funktsiooni prototüüp:void* calloc (size_tnum, size_t size); • Tagastus tüüpi void* - viit allokeeritud mälu algusele • 1. parameeter num (size_t tüüpi) – mitu elementi • 2.parameeter size (size_t tüüpi) – mitu baiti on üks element • Erinevalt malloc() funktsioonist, initsialiseerib calloc() kogu küsitud mälu nullidega!Realisatsioon:ptr=(int*)calloc(n, sizeof(int));

  6. Näide 2 - calloc intmain(void){ int*ptr,i; ptr=(int*)calloc(5,sizeof(int)); for(i = 0; i < n; i++) printf("%d\n", *(ptr+ i)); return 0; }

  7. Näide 3 – calloc vs malloc #include <stdio.h> #include <stdlib.h> intmain(void) { int*ptr1, *ptr2, i; ptr1 = (int*)calloc(5, sizeof(int)); ptr2 = (int*)malloc(5 * sizeof(int)); for(i= 0; i< 5; i++) printf("%d\n", *(ptr1 + i)); for(i= 0; i< 5; i++) printf("%d\n", *(ptr2 + i)); return 0; }

  8. Mälu andmine struktuuri elementidele (pseudo) typedefstruct{ intnum; char *name; } identity; { identity *person; person = (identity*)calloc(1, sizeof(identity)); person->name = (char*)malloc(strlen(buf) + 1); free(person->name); free(person); }

  9. Tunnitöö • Loe sisse N kirjet, mälu anna calloc() funktsiooniga • Sisseloetava struktuuri vorming: • Identifikaator (täisarv) (auto_increment) • Nimi (string) • Vanus (täisarv) • Struktuuri sees olevad stringid peavad olema char* tüüpi (char viidad) ja neile tuleb eraldi mälu allokeerida • Leida ja kuvada isikud kasutaja poolt klaviatuurilt sisestatud vanusevahemikus (x<= vanus <= y) • Ära unusta mälu allokeerimise kontrolli ja kogu küsitud mälu vabastamist (sh struktuuri sees olev char*)

More Related