Introduction to Pointers in C and C++. What is pointer
zebamalik
6 views
18 slides
Oct 12, 2024
Slide 1 of 18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
About This Presentation
Pointers in C++
Size: 6.08 MB
Language: en
Added: Oct 12, 2024
Slides: 18 pages
Slide Content
Comp 401 Memory Representation of Primitive Values and Objects Instructor: Prasun Dewan
Storing Primitive Values and Variables variables memory 5 16 address int i = 5; int i 52 5 Memory Block Memory blocks are of the same size 32 bits 32 bits
Storing Primitive Values and Variables variables memory 5.5 8 address double d = 5.5; double d 48 5.5 Memory Block Memory blocks are of the same size 64 bits 64 bits double e = d; 80 double e 5.5
Storing Primitive Values Values and variables of same type take same amount of fixed storage. The storage consists of one or more consecutive memory words that together form a memory block. Values and variables of different types may take different storage.
Storing Objects Can we assign all variables and objects of the same object type the same sized memory block? No: A variable can be assigned instances of different classes. Different instances of the same class can take different amount of space.
Instance-Specific Structure new AnAnotherLine ( new APolarPoint (14.01, 0.78), 20, 20) AnAnotherLine APolarPoint int int location height width double angle radius double
Instance-Specific Structure AnAnotherLine ACartesianPoint int int location height width int x y int new AnAnotherLine ( new ACartesianPoint (10, 10), 20, 20) Structures of instances of same class can be different!
Storing Object Values and Variables variables memory address Point p1 = new ACartesianPoint (50,100); ACartesianPoint@8 8 50 100 public class ACartesianPoint implements Point { int x int y; … } Instance variables stored in memory Point p1 52 8 Address of object copied to block Memory blocks are of different size! Object variables are pointers to memory blocks
Assignment of Object Variables variables memory address Point p1 = new ACartesianPoint (50,100); ACartesianPoint@8 8 50 100 Point p2 = p1; Point p1 52 8 Point p2 56 8 ACartesianPoint@8 p2 p1
Assignment of Object Variables variables memory address Point p1 = new ACartesianPoint (50,100); ACartesianPoint@8 8 50 100 Point p2 = p1; Point p1 52 8 Point p2 56 8 ACartesianPoint@8 p2 p1 p1.setX(100); 100 p2.getX(); 100 8
Assignment of Object Variables variables memory address Point p1 = new ACartesianPoint (50,100); ACartesianPoint@8 8 50 100 Point p2 = p1; Point p1 52 8 Point p2 56 ACartesianPoint@8 p2 p1 p1.setX(100); 100 p2.getX(); 100 8 Point p1 = new ACartesianPoint (150,75); ACartesianPoint@76 76 50 75 150 76 ACartesianPoint@76 p2.getX(); 100
Extra Slides
Storing Primitive Values and Variables variables memory 5 16 address int i = 5; int i 52 5 Memory Block Memory blocks are of the same size 32 bits 32 bits
Storing Primitive Values and Variables variables memory 5.5 8 address double i = 5.5; double d 48 5.5 Memory Block Memory blocks are of the same size 64 bits 64 bits double e = d; 80 double e 5.5 5.5
Storing Object Values and Variables variables memory address Point p1 = new ACartesianPoint (50,100); ACartesianPoint@8 8 50 100 public class ACartesianPoint implements Point { int x int y; … } Instance variables stored in memory Point p1 52 8 Address of object copied to block Memory blocks are of different size! Object variables are pointers to memory blocks
Assignment of Object Variables variables memory address Point p1 = new ACartesianPoint (50,100); ACartesianPoint@8 8 50 100 Point p2 = p1; Point p1 52 8 Point p2 56 8
Assignment of Object Variables ACartesianPoint@8 p2 p1