Top Banner
Click to add Text Alokasi Memori Yuliana Setiowati
25

Dynamic memory alocation technique

Jul 14, 2016

Download

Documents

Tri Awan

with c/c++, will show how dynamic memory, will be allocated, so we able to manage computer memory, to get efficient and effectively.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dynamic memory alocation technique

Click to add Text

Alokasi Memori

Yuliana Setiowati

Page 2: Dynamic memory alocation technique

Alokasi Memori Menyediakan fasilitas untuk membuat

ukuran buffer dan array secara dinamik.• Dinamik artinya bahwa ruang dalam

memori akan dialokasikan ketika program dieksekusi (run time)

• Fasilitas ini memungkinkan user untuk membuat tipe data dan struktur dengan ukuran dan panjang berapapun yang disesuaikan dengan kebutuhan di dalam program.

Page 3: Dynamic memory alocation technique

sizeof()• Untuk mendapatkan ukuran dari berbagai tipe data, variabel ataupun struktur. •Return value : ukuran dari obyek yang bersangkutan dalam byte. •Parameter dari sizeof() : sebuah obyek atau sebuah tipe data

Page 4: Dynamic memory alocation technique

main()

{

    int myInt;

    Employee john;

    printf("Size of int is %d\n",sizeof(myInt));

    printf("Size of int is %d\n",sizeof(int));

    printf("Size of Employee is %d\n",sizeof(Employee));

    printf("Size of john is %d\n",sizeof(john));

    printf("Size of char is %d\n",sizeof(char));

    printf("Size of short is %d\n",sizeof(short));

    printf("Size of int is %d\n",sizeof(int));

    printf("Size of long is %d\n",sizeof(long));

    printf("Size of float is %d\n",sizeof(float));

    printf("Size of double is %d\n",sizeof(double));

return 0;

}

typedef struct employee_st {

    char name[40];

    int id;

} Employee;

Page 5: Dynamic memory alocation technique

Hasil Program

Page 6: Dynamic memory alocation technique

Fungsi malloc()• Untuk mengalokasikan memori • Bentuk prototypenya adalah void *malloc(int jml_byte);

Return value : • Berhasil : sebuah pointer yang tak bertipe (pointer to void) yang menunjuk ke buffer yang dialokasikan • Gagal : return value berupa sebuah pointer NULL.• Pointer tersebut haruslah dikonversi kepada tipe yang sesuai (dengan menggunakan type cast) agar bisa mengakses data yang disimpan dalam buffer.

Page 7: Dynamic memory alocation technique

main()

{

char s1[] = "This is a sentence";

  char *pblok;

    pblok = (char *) malloc(strlen(s1) + 1);

   if (pblok == NULL) printf("Error on malloc\n");

    else {

   strcpy(pblok,s1);

    printf("s1: %s\n", s1);

    printf("pblok: %s\n", pblok);

return 0;

}

 }

Page 8: Dynamic memory alocation technique

malloc( ) int *p; double *q; Date *r; p = (int*) malloc(sizeof(int)); q = (double*) malloc(sizeof(double)); r = (Date*) malloc(sizeof(Date));

p

q

r3*p

3.14*q

2*r

2 2002

Page 9: Dynamic memory alocation technique

malloc( ), closer lookint *p; p = (int*) malloc(sizeof(int));

sizeof(int) = the number of bytes occupied by an int

Malloc mengalokasikan storage dengan ukuran 4 byte (int)

p = (int*) malloc(…) Pointer tersebut haruslah dikonversi kepada tipe yang sesuai

Page 10: Dynamic memory alocation technique

malloc( )#include <stdlib.h>int main () { int *p; int a=2; p = (int*) malloc(sizeof(int)); *p = 4; *p += a; … free(p); p = NULL;}

p

p*p

?

Menghapus sel yang ditunjuk p

Page 11: Dynamic memory alocation technique

Fungsi free()• Jika bekerja dengan menggunakan memori yang dialokasikan secara dinamis, maka memori harus dibebaskan kembali setelah selesai digunakan untuk dikembalikan kepada sistem. • Setelah suatu ruang memori dibebaskan, ruang tersebut bisa dipakai lagi untuk alokasi variabel dinamis lainnya. • Bentuk deklarasi

void free(void *pblok);

dengan pblok adalah pointer yang menunjuk ke memori yang akan dibebaskan. • Dalam hal ini, pointer pblok tidak perlu di-cast kembali ke void terlebih dahulu. Compiler otomatis telah menangani proses ini.

Page 12: Dynamic memory alocation technique

Fungsi free()main(){

char *pblok; pblok = (char *) malloc(500 * sizeof(char)); if (pblok == NULL)

puts("Error on malloc");else { puts("OK, alokasi memori sudah dilakukan");

puts("------");free(pblok);puts("Blok memori telah dibebaskan

kembali");}

}

Page 13: Dynamic memory alocation technique

free( )#include <stdlib.h>int main () { int *p; int a=2; p = (int*) malloc(sizeof(int)); *p = 4; *p += a; … free(p); p = NULL;}

Pada saat variabel dinamik tidak digunakan lagi kita perlu membebaskannya. Kompiler tidak mendealokasi storage space secara otomatis

p

p*p

p ?

?

Menghapus sel yang ditunjuk p

Page 14: Dynamic memory alocation technique

NULL#include <stdlib.h>int main () { int *p; int a=2; p = (int*) malloc(sizeof(int)); *p = 4; *p += a; … free(p); p = NULL;}

p

p*p

p ?

?

p

Page 15: Dynamic memory alocation technique

#include <stdlib.h>#include <stdlib.h>int main () { int *p; int a=2; p = (int*) malloc(sizeof(int)); *p = 4; *p += a; … free(p); p = NULL;}

Menggunakan fungsi malloc(), free() dan NULL harus mengincludekan stdlib.h.

Page 16: Dynamic memory alocation technique

*q = *p

int *p, *q;p = (int*)malloc(sizeof(int));q = (int*)malloc(sizeof(int));*p = 3;*q = *p;

3p

q*q

*p

3

Page 17: Dynamic memory alocation technique

q = p

int *p, *q;p = (int*) malloc(sizeof(int));q = (int*) malloc(sizeof(int));*p = 3; q = p;

3p

q*q

*p

3*q

*pp

q

Page 18: Dynamic memory alocation technique

Example#include <stdlib.h>int main () { int *p, *q; int *r; p = (int*) malloc(sizeof(int)); r = p; q = (int*) malloc(sizeof(int)); *p = 3; *q = *p + *r; (A) free(r); (B) r = NULL; (C)}

3r6q *q

*pp*r

(A)

(B)r

6q *q

p?

(C)r

6q *q

p?

Page 19: Dynamic memory alocation technique

Latihan#include <stdlib.h>int main () { int *p, *q; int *r; int *s; p = (int*) malloc(sizeof(int)); r = p; *p = 5; p = (int*) malloc(sizeof(int)); q = p; s = r; *q = *s + 1; *r = *p * 2; (A) free(r); r = NULL; free(q); q = NULL; (B)}

Page 20: Dynamic memory alocation technique

Memory Leak#include <stdlib.h>int main () { int *p, *q; p = (int*) malloc(sizeof(int)); *p = 1+2; (A)

p = (int*) malloc(sizeof(int)); scanf("%d", p); (B) free(p); p = NULL; (C) }

3p *p

3p

*p8

(A)

(B)

3p(C)

q = p; or free(p);

Pada saat p=NULL; maka *p tidak dapat diakses lagi

Page 21: Dynamic memory alocation technique

Pointer yang tidak valid#include <stdlib.h>int main () { int *p, *q; p = (int*) malloc(sizeof(int)); q = p; *q = 3; (A) free(q); q = NULL; (B)

}

3q*pp

*q

(A)

Setelah free(q), sel *p (dan *q) tidak ada. Oleh sebab itu kita tidak dapat menggunakan *p atau *q.

(B)qp

?printf("%d %d", *p, *q);*p = 999;*q = 999;

Page 22: Dynamic memory alocation technique

Free sel yang tidak digunakan hanya satu kali

#include <stdlib.h>int main () { int *p, *q; p = (int*) malloc(sizeof(int)); q = p; *q = 3; (A) free(q); q = NULL; (B)

p = NULL; (C)}

3q*pp

*q

(A)

Free untuk tiap sel yang tidak digunakan hanya dilakukan satu kali (Bukan free tiap pointer satu kali)

(B)qp

?

free(p); (C)qp

Page 23: Dynamic memory alocation technique

Jangan Free() Variabel Static#include <stdlib.h>int main () { int *p, *q; int a = 5; p = (int*) malloc(sizeof(int)); q = &a; *p = 3; (A) free(p); p = NULL; (B)

q = NULL; (C)}

Kita hanya perlu membebaskan variabel dinamik. Bukan variabel biasa. Ingat Free() untuk malloc() saja.

free(q);

5q

*pp*q

(A) 3

a

(B)qp

5 *qa

(C)qp

5a

Page 24: Dynamic memory alocation technique

Fungsi realloc()• Untuk mengalokasikan ulang memori yang dipesan • Fungsi ini akan mengalokasikan kembali pointer yang sebelumnya telah diatur untuk menunjuk sejumlah lokasi, memberinya ukuran yang baru (bisa jadi lebih kecil atau lebih besar). • Bentuk deklarasi :...

pblok = (char *) malloc(500 * sizeof(char));

...

pblok = realloc(pblok, 600 * sizeof(char));

Page 25: Dynamic memory alocation technique

Fungsi realloc()• fungsi ini memberikan return value berupa pointer yang sama, meng-copy data lama ke lokasi baru dan mengarahkan pointer ke sejumlah lokasi baru tersebut • membebaskan blok memori yang lama. • Jika berhasil : melakukan relokasi baru.• Jika tidak berhasil : return value berupa NULL. • Cara terbaik adalah mengecek hasil realloc() untuk memastikan hasilnya bukan NULL pointer, sebelum kemudian menumpuki pointer lama dengan return value dari realloc().