Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Pointer Implementasi pointer dalam pengalokasian memori SUMBER : Struktur Data /3sks/Semester II (KELAS MALAM) Tahun Akademik 2012 - 2013 –

Presentasi serupa


Presentasi berjudul: "Pointer Implementasi pointer dalam pengalokasian memori SUMBER : Struktur Data /3sks/Semester II (KELAS MALAM) Tahun Akademik 2012 - 2013 –"— Transcript presentasi:

1 Pointer Implementasi pointer dalam pengalokasian memori SUMBER : 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” “program = algorithm + data structure”

2 Struktur Data 2 Kerjakan soal no 2.a Apa ya pointer itu ?

3

4

5

6

7

8 Struktur Data 8 : 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

9 9 : (&ptr) 1500 (&x) 1100 (&y) 1104 : : ptr = 1100 30 : 35 *ptr += 10; 3530 ptrxy int *ptr; int x=25; Int y=30; : (&ptr) 1500 (&x) 1100 (&y) 1104 : : ptr = ? 30 : 25 30 ptrxy ptr = &x; : (&ptr) 1500 (&x) 1100 (&y) 1104 : : ptr = 1100 30 : 25 30 ptrxy

10 10... MEMORY 0 1 2 3 4 5 81345 81346 81347 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

11 11 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

12 12 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

13 13 *ptr = 10; - nilai x berubah - nilai pointer ptr tetap

14 14 int x = 5; int y = 7; int *ptr = &x; // LEGAL int *ptr = &x; // ILLEGAL: x belum dideklarasikan int 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

15 15 #include 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

16 16 … … … 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; }

17 17 #include 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 = Copy Within PassByValue(), now Boys = 6Girls = 9 After calling PassByValue(), within main() Boys = 3Girls = 5 Passing arguments by value = Copy Within PassByValue(), now Boys = 6Girls = 9 After calling PassByValue(), within main() Boys = 3Girls = 5 Passing arguments by reference Within Reference(), now Boys = 6Girls = 9 After calling Reference(), within main() Boys = 6Girls = 9 Passing arguments by reference Within Reference(), now Boys = 6Girls = 9 After calling Reference(), within main() Boys = 6Girls = 9 Passing arguments pointers Within Pointers(), now Boys = 9 Girls = 13 After calling Pointers(), within main() Boys = 9 Girls = 13 Passing arguments pointers Within Pointers(), now Boys = 9 Girls = 13 After 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); Pointers(&Boys, &Girls); Reference(Boys, Girls);

18 Any Questions??

19 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 !

20 Ballighu’ anniy walau aayatun... Sebarkanlah ilmu walau satu ayat... Thank’s for your attention, Surabaya, 05 Maret 2013


Download ppt "Pointer Implementasi pointer dalam pengalokasian memori SUMBER : Struktur Data /3sks/Semester II (KELAS MALAM) Tahun Akademik 2012 - 2013 –"

Presentasi serupa


Iklan oleh Google