Struktur data (alokasi memory)

1,137 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,137
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
41
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Struktur data (alokasi memory)

  1. 1. PointerImplementasi pointer dalam pengalokasian 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 + datastructure”
  2. 2. Struktur Data 2*Kerjakan soalno 2.aApa yapointer itu ?• Pointer tidak menampung data• Pointer menampung alamat dari selmemori lain yang menampung nilai datayang dituju
  3. 3. Struktur Data 8*::Tambahkan 10 ke p:::Tambahkan 10ke datayang alamatnyatersimpan di p:p = 200??::56010001001200::10001001200::p = 210??::55010001001200::p = 200??::550
  4. 4. 9*:(&ptr) 1500(&x) 1100(&y) 1104::ptr = 110030:35*ptr += 10;35 30ptr x yint *ptr;int x=25;Int y=30;:(&ptr) 1500(&x) 1100(&y) 1104::ptr = ?30:2525 30ptr x yptr = &x;:(&ptr) 1500(&x) 1100(&y) 1104::ptr = 110030:2525 30ptr x y
  5. 5. 10*......MEMORY012345813458134681347Addressint foo;int *x;foo = 123;x = &foo;foox1233menyediakan akses tidaklangsung ke suatu obyekobyek untuk mengakses obyeklaindapat mengakses data yangditunjuk oleh pointer dereferencing pointer
  6. 6. 11*pointer harus memiliki nilai sebelum mengakses data yangditunjuk (dereferenced )int *x;*x=3;int foo;int *x;x = &foo;*x=3;&foo = alamat dari foo
  7. 7. 12*int *intPtr;intPtr = new int;*intPtr = 6837;delete intPtr;int otherVal = 5;intPtr = &otherVal;inisialisasi pointeralokasi memory addressset isi dari pointermerubah intPtr ke lokasimemory address lain6837*intPtr0x0050intPtr5*intPtr0x0054intPtrotherVal&otherValmembuang alokasi memory address
  8. 8. 13**ptr = 10;- nilai x berubah- nilai pointer ptrtetap
  9. 9. 14*int x = 5;int y = 7;int *ptr = &x; // LEGALint *ptr = &x; // ILLEGAL: x belumdideklarasikanint x = 5;int y = 7;int x = 5;int y = 7;int *ptr = x; // ILLEGAL: x bukan memoryaddressint x = 5;int y = 7;int *ptr; // LEGAL: ptr belum diinisialisasi
  10. 10. 15*#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 << "ntBoys = " << Boys;cout << "ntGirls = " << Girls;cout << "nPassing arguments by value = Copy";PassByValue(Boys, Girls);cout << "nAfter calling PassByValue(), within main()";cout << "ntBoys = " << Boys;cout << "ntGirls = " << Girls;… … …At startup, within main()Boys = 3Girls = 5
  11. 11. 16*… … …cout << "nPassing arguments by reference";Reference(Boys, Girls);cout << "nAfter calling Reference(), within main()";cout << "ntBoys = " << Boys;cout << "ntGirls = " << Girls;cout << "nPassing arguments pointers";Pointers(&Boys, &Girls);cout << "nAfter calling Pointers(), within main()";cout << "ntBoys = " << Boys;cout << "ntGirls = " << Girls;cout << "n";system("PAUSE");return 0;}
  12. 12. 17*#include <iostream>using namespace std;void PassByValue(int b, int g){b += 3, g += 4;cout << "nWithin PassByValue(), now";cout << "ntBoys = " << b;cout << "ntGirls = " << g;}void Reference(int &b, int &g){b += 3, g += 4;cout << "nWithin Reference(), now";cout << "ntBoys = " << b;cout << "ntGirls = " << g;}void Pointers(int *b, int *g){*b += 3, *g += 4;cout << "nWithin Pointers(), now";cout << "ntBoys = " << *b;cout << "ntGirls = " << *g;}int main(){… … …Passing arguments by value = CopyWithin PassByValue(), nowBoys = 6 Girls = 9After calling PassByValue(), within main()Boys = 3 Girls = 5Passing arguments by referenceWithin Reference(), nowBoys = 6 Girls = 9After calling Reference(), within main()Boys = 6 Girls = 9Passing arguments pointersWithin Pointers(), nowBoys = 9 Girls = 13After calling Pointers(), within main()Boys = 9 Girls = 13At startup, within main()Boys = 3 Girls = 5PassByValue(Boys, Girls);Pointers(&Boys, &Girls);Reference(Boys, Girls);
  13. 13. *DiskusiAny Questions??
  14. 14. *TugasDikumpulkan paling lambat tanggal 11 Maret 2013,pukul 24.00 wib via email saya, format pdf sertakan fotoanda disebelah nama.1. Jelaskan (tracing) dan tentukan outputdari Soal 1 dan Soal 2 !2. Jelaskan perbedaan antara Soal 3 danSoal 4, kemudian tentukan output darikedua soal tersebut !
  15. 15. Ballighu’ anniy walauaayatun . . .Sebarkanlah ilmu walausatu ayat . . .Thank’s for your attention,Surabaya, 05 Maret 2013

×