Struktur data (alokasi memory)

20
Pointer Implementasi pointer dalam pengalokasian memori * ALOKASI MEMORI Struktur Data /3sks/Semester II (KELAS MALAM) Tahun Akademik 2012 - 2013 @Maret 2013 – by M. Mahaputra Hidayat, S.Kom *dirangkum dari berbagai sumber dan ITS “program = algorithm + data structure”

Transcript of Struktur data (alokasi memory)

Page 1: Struktur data (alokasi memory)

Pointer

Implementasi pointer dalam pengalokasian memori

*ALOKASI MEMORI

Struktur Data /3sks/Semester II (KELAS MALAM)Tahun Akademik 2012 - 2013@Maret 2013 – by M. Mahaputra Hidayat, S.Kom*dirangkum dari berbagai sumber dan ITS

“program = algorithm + data structure”

Page 2: Struktur data (alokasi memory)

Struktur Data 2

*Definisi Pointer

Kerjakan soal no 2.a

Apa ya pointer itu ?

• Pointer tidak menampung data• Pointer menampung alamat dari sel memori lain yang menampung nilai data yang dituju

Page 3: Struktur data (alokasi memory)
Page 4: Struktur data (alokasi memory)
Page 5: Struktur data (alokasi memory)
Page 6: Struktur data (alokasi memory)
Page 7: Struktur data (alokasi memory)
Page 8: Struktur data (alokasi memory)

Struktur Data 8

*Ilustrasi Pointer

::Tambahkan 10 ke p::

:Tambahkan 10 ke data yang alamatnya tersimpan di p:

p = 200

??

:

:

560

1000

1001

200:

:

1000

1001

200:

:

p = 210

??

:

:

550

1000

1001

200:

:

p = 200

??

:

:

550

Page 9: Struktur data (alokasi memory)

9

*Cara Penulisan Pointer

:

(&ptr) 1500

(&x) 1100(&y) 1104

:

:

ptr = 1100

30

:

35

*ptr += 10;

35 30

ptr x y

int *ptr;int x=25;Int y=30;

:

(&ptr) 1500

(&x) 1100(&y) 1104

:

:

ptr = ?

30

:

25

25 30

ptr x y

ptr = &x;

:

(&ptr) 1500

(&x) 1100(&y) 1104

:

:

ptr = 1100

30

:

25

25 30

ptr x y

Page 10: Struktur data (alokasi memory)

10

*Definisi Pointer

...

...

MEMORY

012345

813458134681347

Address

int foo;int *x;

foo = 123;x = &foo;

foo

x

123

3

menyediakan akses tidak langsung ke suatu obyek obyek untuk mengakses obyek lain dapat mengakses data yang ditunjuk oleh pointer

dereferencing pointer

Page 11: Struktur data (alokasi memory)

11

*Set nilai ke dereferenced pointer

pointer harus memiliki nilai sebelum mengakses data yang ditunjuk (dereferenced )

int *x;*x=3;

int foo;int *x;x = &foo;*x=3;

ERROR!!!

x belum menunjuk

suatu obyek!!! LEGAL.

x menunjuk ke foo

&foo = alamat dari foo

Page 12: Struktur data (alokasi memory)

12

*Penggunaan pointer

int *intPtr;

intPtr = new int;

*intPtr = 6837;

delete intPtr;

int otherVal = 5;intPtr = &otherVal;

inisialisasi pointer

alokasi memory address

set isi dari pointer

merubah intPtr ke lokasi memory address lain

6837*intPtr

0x0050 intPtr

5*intPtr

0x0054 intPtr

otherVal

&otherVal

membuang alokasi memory address

Page 13: Struktur data (alokasi memory)

13

*Penggunaan pointer

*ptr = 10;*ptr = 10;

- nilai x berubah

- nilai pointer ptr tetap

Page 14: Struktur data (alokasi memory)

14

*Penggunaan pointer

int x = 5;int y = 7;int *ptr = &x; // LEGAL

int *ptr = &x; // ILLEGAL: x belum dideklarasikanint x = 5;int y = 7;

int x = 5;int y = 7;int *ptr = x; // ILLEGAL: x bukan memory address

int x = 5;int y = 7;int *ptr; // LEGAL: ptr belum diinisialisasi

Page 15: Struktur data (alokasi memory)

15

*Contoh Passing parameter#include <iostream>using namespace std;… … …int main(){

int Boys = 3, Girls = 5;

void PassByValue(int males, int females);void Reference(int &m, int &f);void Pointers(int *u, int *v);

cout << "At startup, within main()";cout << "\n\tBoys = " << Boys;cout << "\n\tGirls = " << Girls;

cout << "\nPassing arguments by value = Copy";PassByValue(Boys, Girls);cout << "\nAfter calling PassByValue(), within main()";

cout << "\n\tBoys = " << Boys;cout << "\n\tGirls = " << Girls;

… … …

At startup, within main() Boys = 3 Girls = 5

At startup, within main() Boys = 3 Girls = 5

Page 16: Struktur data (alokasi memory)

16

*Contoh Passing parameter… … …

cout << "\nPassing arguments by reference";Reference(Boys, Girls);cout << "\nAfter calling Reference(), within main()";

cout << "\n\tBoys = " << Boys;cout << "\n\tGirls = " << Girls;

cout << "\nPassing arguments pointers";Pointers(&Boys, &Girls);cout << "\nAfter calling Pointers(), within main()";cout << "\n\tBoys = " << Boys;cout << "\n\tGirls = " << Girls;cout << "\n";

system("PAUSE"); return 0;

}

Page 17: Struktur data (alokasi memory)

17

*Contoh Passing parameter#include <iostream>

using namespace std;void PassByValue(int b, int g){

b += 3, g += 4;cout << "\nWithin PassByValue(), now";cout << "\n\tBoys = " << b;cout << "\n\tGirls = " << g;

}void Reference(int &b, int &g){

b += 3, g += 4;cout << "\nWithin Reference(), now";cout << "\n\tBoys = " << b;cout << "\n\tGirls = " << g;

}void Pointers(int *b, int *g){

*b += 3, *g += 4;cout << "\nWithin Pointers(), now";cout << "\n\tBoys = " << *b;cout << "\n\tGirls = " << *g;

}int main(){… … …

Passing arguments by value = CopyWithin PassByValue(), now Boys = 6 Girls = 9After calling PassByValue(), within main() Boys = 3 Girls = 5

Passing arguments by value = CopyWithin PassByValue(), now Boys = 6 Girls = 9After calling PassByValue(), within main() Boys = 3 Girls = 5

Passing arguments by referenceWithin Reference(), now Boys = 6 Girls = 9After calling Reference(), within main() Boys = 6 Girls = 9

Passing arguments by referenceWithin Reference(), now Boys = 6 Girls = 9After calling Reference(), within main() Boys = 6 Girls = 9

Passing arguments pointersWithin Pointers(), now Boys = 9 Girls = 13After calling Pointers(), within main() Boys = 9 Girls = 13

Passing arguments pointersWithin Pointers(), now Boys = 9 Girls = 13After calling Pointers(), within main() Boys = 9 Girls = 13

At startup, within main() Boys = 3 Girls = 5

At startup, within main() Boys = 3 Girls = 5

PassByValue(Boys, Girls);

PassByValue(Boys, Girls);

Pointers(&Boys, &Girls);

Pointers(&Boys, &Girls);

Reference(Boys, Girls);

Reference(Boys, Girls);

Page 18: Struktur data (alokasi memory)

*Diskusi

Any Questions??

Page 19: Struktur data (alokasi memory)

*TugasDikumpulkan paling lambat tanggal 11 Maret 2013, pukul 24.00 wib via email saya, format pdf sertakan foto anda disebelah nama.

1. Jelaskan (tracing) dan tentukan output dari Soal 1 dan Soal 2 !

2. Jelaskan perbedaan antara Soal 3 dan Soal 4, kemudian tentukan output dari kedua soal tersebut !

Page 20: Struktur data (alokasi memory)

Ballighu’ anniy walau aayatun . . .

Sebarkanlah ilmu walau satu ayat . . .

Thank’s for your attention,Surabaya, 05 Maret 2013