Pointer
Implementasi 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 + data
structure”
Struktur Data 2
*
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
Struktur data (alokasi memory)
Struktur data (alokasi memory)
Struktur data (alokasi memory)
Struktur data (alokasi memory)
Struktur data (alokasi memory)
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
*
:
(&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
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
*
pointer harus memiliki nilai sebelum mengakses data yang
ditunjuk (dereferenced )
int *x;
*x=3;
int foo;
int *x;
x = &foo;
*x=3;
&foo = alamat dari foo
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
0x0050intPtr
5*intPtr
0x0054intPtr
otherVal
&otherVal
membuang alokasi memory address
13
*
*ptr = 10;
- nilai x berubah
- nilai pointer ptr
tetap
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
*
#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 = 3
Girls = 5
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;
}
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 = Copy
Within PassByValue(), now
Boys = 6 Girls = 9
After calling PassByValue(), within main()
Boys = 3 Girls = 5
Passing arguments by reference
Within Reference(), now
Boys = 6 Girls = 9
After calling Reference(), within main()
Boys = 6 Girls = 9
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
PassByValue(Boys, Girls);
Pointers(&Boys, &Girls);
Reference(Boys, Girls);
*Diskusi
Any Questions??
*Tugas
Dikumpulkan 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 !
Ballighu’ anniy walau
aayatun . . .
Sebarkanlah ilmu walau
satu ayat . . .
Thank’s for your attention,
Surabaya, 05 Maret 2013

More Related Content

DOCX
Ejercicios de programacion
PDF
Clang2018 class3
DOC
oop Lecture 4
PPTX
Introduction to F# for the C# developer
DOCX
Assignement of programming & problem solving u.s ass.(1)
PDF
Ooprc4 b
Ejercicios de programacion
Clang2018 class3
oop Lecture 4
Introduction to F# for the C# developer
Assignement of programming & problem solving u.s ass.(1)
Ooprc4 b

What's hot (13)

PDF
Lec23-CS110 Computational Engineering
PPTX
Pointer example
PDF
6. function
RTF
PDF
C++ TUTORIAL 3
PPTX
Computer lab (programs)
PDF
The solution manual of c by robin
DOCX
Include
PPT
2nd Test - Parent Functions
DOCX
Python real time tutorial
PDF
Phing - PHP Conference 2015
PDF
Python Basics #1
Lec23-CS110 Computational Engineering
Pointer example
6. function
C++ TUTORIAL 3
Computer lab (programs)
The solution manual of c by robin
Include
2nd Test - Parent Functions
Python real time tutorial
Phing - PHP Conference 2015
Python Basics #1
Ad

Viewers also liked (7)

PDF
Algoritma dan Struktur Data - Rekursif
PPTX
Bab 8 struktur rekursif
PDF
Double linked list
PPT
Bab 8 rekursif
PPTX
Efisiensi algoritma
PDF
Pointer c++
PDF
Fathoni m bahan ajar_if2018_prak.struktur data
Algoritma dan Struktur Data - Rekursif
Bab 8 struktur rekursif
Double linked list
Bab 8 rekursif
Efisiensi algoritma
Pointer c++
Fathoni m bahan ajar_if2018_prak.struktur data
Ad

Similar to Struktur data (alokasi memory) (10)

PPT
Lec 37 - pointers
PPT
Lec 36 - pointers
PPT
FP 201 - Unit 6
PDF
C++ L06-Pointers
PPTX
Object Oriented Programming using C++: Ch10 Pointers.pptx
PPT
Lec 40.41 - pointers
DOCX
Tugas praktikukm pemrograman c++
PPTX
Chp3(pointers ref)
ODP
Pointers in c++ by minal
PPT
CPointer-hk.ppt
Lec 37 - pointers
Lec 36 - pointers
FP 201 - Unit 6
C++ L06-Pointers
Object Oriented Programming using C++: Ch10 Pointers.pptx
Lec 40.41 - pointers
Tugas praktikukm pemrograman c++
Chp3(pointers ref)
Pointers in c++ by minal
CPointer-hk.ppt

Recently uploaded (20)

PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Five Habits of High-Impact Board Members
PPT
Geologic Time for studying geology for geologist
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PPTX
Chapter 5: Probability Theory and Statistics
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Unlock new opportunities with location data.pdf
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
Modernising the Digital Integration Hub
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
NewMind AI Weekly Chronicles – August ’25 Week III
A contest of sentiment analysis: k-nearest neighbor versus neural network
Five Habits of High-Impact Board Members
Geologic Time for studying geology for geologist
sustainability-14-14877-v2.pddhzftheheeeee
A comparative study of natural language inference in Swahili using monolingua...
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
1 - Historical Antecedents, Social Consideration.pdf
Hybrid model detection and classification of lung cancer
Taming the Chaos: How to Turn Unstructured Data into Decisions
Chapter 5: Probability Theory and Statistics
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Unlock new opportunities with location data.pdf
O2C Customer Invoices to Receipt V15A.pptx
Final SEM Unit 1 for mit wpu at pune .pptx
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Modernising the Digital Integration Hub
A novel scalable deep ensemble learning framework for big data classification...
Web Crawler for Trend Tracking Gen Z Insights.pptx

Struktur data (alokasi memory)

  • 1. Pointer Implementasi 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 + data structure”
  • 2. Struktur Data 2 * 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
  • 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; 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
  • 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; &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 0x0050intPtr 5*intPtr 0x0054intPtr 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 <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 = 3 Girls = 5
  • 16. 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; }
  • 17. 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 = Copy Within PassByValue(), now Boys = 6 Girls = 9 After calling PassByValue(), within main() Boys = 3 Girls = 5 Passing arguments by reference Within Reference(), now Boys = 6 Girls = 9 After calling Reference(), within main() Boys = 6 Girls = 9 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 PassByValue(Boys, Girls); Pointers(&Boys, &Girls); Reference(Boys, Girls);
  • 19. *Tugas Dikumpulkan 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 !
  • 20. Ballighu’ anniy walau aayatun . . . Sebarkanlah ilmu walau satu ayat . . . Thank’s for your attention, Surabaya, 05 Maret 2013