Download presentation
Presentation is loading. Please wait.
1
Pointers Re-introduce Pointer Concepts
2
Why Pointers Remember Arrays – try to accommodate user entry by guessing the size. Usage increases over time – our array size becomes to small Try to outguess by creating array’s – waste memory and performance How can we create an array that is just the right size every time we run our program?
3
Why Cont. Compiler error –cout << “Enter # of entries you will provide: “; –cin << size; –int array [size]; Solution -> pointers; we want to make the array only as large as needed during execution Later if more RAM is needed – it is obtained from the OS while program is running.
4
Why Cont. When OS hands out RAM, it does so by providing a starting address of that location. Program needs to store this address in some variable so that it can gain access to RAM A pointer is a variable that can store a memory address. Hence – one purpose for pointers.
5
Why Cont. Another – as you learn about inheritance and polymorphism – see that process objects uniformly – need to store their addresses in an array. Need an array of pointers to store the addresses Pointers enable virtual functions – advanced C++ skills. A programmer who understands pointers realizes what is going on “behind the scenes”. Pointers are used by OS and help us to understand how it operates.
6
An Analogy In an apartment – post office box - # 415 Mail is delivered to that box # specific to its owner When create a variable – int x = 54; the compiler assigns an address ( PO box #) to that variable. Memory is RAM (Random Access Memory) It selects the # -- similar to PO selecting PO # Once compiler stores X – you can not tell it to move to another location in memory.
7
Analogy Cont. However, the mail changes from day to day in same way data placed in your variable changes Name of variable and address are fixed once the program starts executing, but contents will change.
8
Understanding Pointers One way is to understand Assembly language Simple computer system contains RAM / CPU Ram – memory – where computer instructions, data, other items are stored Ram made up of locations, each location is accessed by its address – like PO Box.
9
Ram (Prog Stored) Ram (Data Stored)CPU Address Contents of Location FFOO Move the address FFFO to the register called pointer FF10 Move the number that is in the location specified by the address that is in pointer to the accumulator. FF20 Add 1 to the contents of pointer FF30 Add 1 to the contents of accumulator FF40 Move the number that is in the accumulator to the location specified by the address in the pointer. FFF0 FFF1 FFF0FFF1 FFF0 4 FFF1 FFF0FFF1 FFF0FFF1 5 FFF0 4 FFF1 5 pointeraccumulator
10
Ram (Prog Stored) Ram (Data Stored)CPU Address Contents of Location FFOO pointer = &i; FF10 accumulator = *pointer; FF20 ++pointer; FF30 ++accumulator FF40 *pointer = accumulator FFF0 FFF1 FFF0FFF1 FFF0 4 FFF1 FFF0FFF1 FFF0FFF1 5 FFF0 4 FFF1 5 pointeraccumulator ij
11
De-reference accumulator = *pointer; Two step: 1: Find the address stored in pointer, FFF0. Step 2: Go to that address (FFF0) and find what is stored there. int *pointer; {Declaring the pointer} This is the name of the variable That stores addresses Of integers
12
Proper Use of Pointers Recall – variables that store memory addresses are called pointers. Pointers allow us to access variables ( and memory) indirectly. Int I, *p; - two variables I and p –I stores integer values while p stores addresses of integer locations. –You can store 54 in I but not in p or you can store the address of I in p but not in I.
13
Proper Use Cont. X = 54; //storing a integer value P = & x; //storing an address of an integer variable RAM AD1A03 AD1A0454i AD1A05AD1A04p
14
Proper Use Cont. Also define pointer variables that store addresses to other data types: –Char a[11], *pa; –Float x, *px; pa can store addresses of characters and px can store addresses of floats pa = &a[10]; //storing a char address in a char ptr px = &x; //storing a float address in a float ptr This is wrong: pa = &x; and px = 3.40
15
Proper Use Cont. 54 is an integer constant value 54 will always be the same I is a integer variable its value may change Address AD1A05 is a pointer constant It is an address of a location in RAM – can not be changed however p is a pointer variable; the address stored in it may change
16
Let’s Review Int I = 5, j = 7, *p = &I, *q = &j; Float x = 2.0, y = 8.0, *r = &x, *s = &y; NonPointersPointers Ints I jp q 5 7 FFOO FF10 FFOO FF10 Floats x yr s 2.0 8.0 FFA0 FFB0 FFA0 FFB0
17
Valid Statments Statement # Valid Statements Variable Changed To What Value 1I = j;I 7 2I = I – x;I3 3I = *r;I2 4p = & j; pFF10 5 p = q;p FF10 6*p = j;I7 7*p = *s;I8
18
Invalid Statements Statement #Invalid Statements Error 8p = j;Can’t place an int value in a ptr 9p = * j;can’t de-reference an int value 10p = s; can’t place a float address in an int ptr 11p = *q; can’t place an int value in a ptr 12p = &x; can’t place a float address in a int ptr 13 *p = s; can’t place a ptr in a non-ptr variable, i 14 *p = & i; can’t place an address in a non-ptr variable Notice that 14 is wrong as an executable statement but is correct when used in a declaration int *p = &i;
19
Arrays & Pointers Whether you realized it or not –we’ve been working with ptrs. Passing array’s to functions – we were actually passing its starting address Working with addresses is working with pointers
20
Example Int i = 20, bal[ 5 ] = {500, 200, 400, 100, 700}, *p = &i; i bal[0] [1] [2] [3] [4] bal p 20 500 200 400 100 700 FFA4 FFA0 FFA0 FFA4 the address in RAM where bal[] begins Bal and p are not any different both int ptrs Can we assign bal to p ? Yes Bal contains an int address and p is an int ptr.
21
Exp Cont. Can you assign bal [0] to p – also yes. Bal and & bal[0] are both evaluated to the starting address of the array. P = bal; //valid and p = &bal[0] /// also valid Referencing to any array name is the same as the address of the first element of that array Now can we assign p to bal No! Difference - once the array is created by the compiler, it cannot be moved in memory. Its starting address is fixed. P on the other hand isn’t used to declare any array so the address in p can be modified. Note: pointers are pointer variables and arrays are pointer constants. So bal = p ; //invalid
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.