210 likes | 327 Views
Pointers. Re-introduce Pointer Concepts. 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
E N D
Pointers Re-introduce Pointer Concepts
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?
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.
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.
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.
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.
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.
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.
Ram (Data Stored) CPU Ram (Prog Stored) pointer accumulator Address Contents of Location FFF0 4 FFF1 FFF0 FFOO Move the address FFFO to the register called pointer FFF0 FFF1 4 FFF0 FFF1 FF10 Move the number that is in the location specified by the address that is in pointer to the accumulator. FFF1 FFF0 FFF1 5 FF20 Add 1 to the contents of pointer FFF0 FFF1 5 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.
Ram (Data Stored) CPU Ram (Prog Stored) i j pointer accumulator Address Contents of Location FFF0 4 FFF1 FFF0 FFOO pointer = &i; FFF0 FFF1 4 FFF0 FFF1 FF10 accumulator = *pointer; FFF1 FFF0 FFF1 5 FF20 ++pointer; FFF0 FFF1 5 FF30 ++accumulator FF40 *pointer = accumulator
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
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.
Proper Use Cont. • X = 54; //storing a integer value • P = & x; //storing an address of an integer variable
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
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
Let’s Review • Int I = 5, j = 7, *p = &I, *q = &j; • Float x = 2.0, y = 8.0, *r = &x, *s = &y; NonPointers Pointers Ints I j p q 5 7 FFOO FF10 FFOO FF10 Floats x y r s 2.0 8.0 FFA0 FFB0 FFA0 FFB0
Valid Statments Statement # Valid Statements Variable Changed To What Value 1 I = j; I 7 2 I = I – x; I 3 3 I = *r; I 2 4 p = & j; p FF10 5 p = q; p FF10 6 *p = j; I 7 7 *p = *s; I 8
Invalid Statements Statement # Invalid Statements Error 8 p = j; Can’t place an int value in a ptr 9 p = * j; can’t de-reference an int value 10 p = s; can’t place a float address in an int ptr 11 p = *q; can’t place an int value in a ptr 12 p = &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;
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
Example Int i = 20, bal[ 5 ] = {500, 200, 400, 100, 700}, *p = &i; i bal[0] [1] [2] [3] [4] bal p • 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.
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