270 likes | 343 Views
EEE 243B Applied Computer Programming. 13 - Pointers and Addresses §9.1 Prof Sylvain P. Leblanc. Review. What is a derived type? Why are arrays a derived type? What are the three characteristics of an array? What is meant by pass by value and pass by reference? . Outline. Addresses:
E N D
EEE 243BApplied Computer Programming 13 - Pointers and Addresses §9.1 Prof Sylvain P. Leblanc
Review • What is a derived type? • Why are arrays a derived type? • What are the three characteristics of an array? • What is meant by pass by value and pass by reference? Prof S.P. Leblanc
Outline • Addresses: • symbolic, logical and physical memory model • The address operator: & • Pointers • Indirection operator: * • Initialization of pointers • Working with pointers and addresses • Pointer Types Prof S.P. Leblanc
1. Addresses • So far, we have used variables to identify data within our programs • We assigned an identifier (a symbol) to our data in a declaration, and then we used this identifier to manipulate this data • The compiler uses these identifiers to resolve the symbolic location of a variable to an address Prof S.P. Leblanc
1. Addresses • When we talked about arrays and functions, we saw that passing a reference to a variable instead of passing its value can be more efficient • That is because a function can access and modify “outside” data directly • Thus overhead associated with function calls • There are other uses for the more direct access by reference Prof S.P. Leblanc
1. Addresses • First a note: modern operating systems load programs into physical memory at locations of their choosing • For one execution, variable a might be at physical address 01A4:F4A1 and at the next execution the same variable could be at 34BC:4DA1 • So although we want the power of using addresses, we do not want to lose the flexibility afforded by symbolic names Prof S.P. Leblanc
1. Addresses • Well, you are in luck, C allows us to use symbolic addresses using something called a pointer. • But before we go further, we will look at a memory model to sort out all this addressing lingo Prof S.P. Leblanc
1. Addresses – Memory Model Logical Physical Symbolic 0000 #define stuff … void main() { … int a; … } 00FF … Compile and link Load Prof S.P. Leblanc
1. Addresses – Memory Abstraction • The model we just saw can be simplified if we understand that we can obtain a physical address at run time, from our program in the symbolic world (the C code) • The symbolic addresses hide the intricacies of the operating system and the addressing hardware • Sales pitch: if you take the operating systems course in 4th year, you will see the dirt under the rug Prof S.P. Leblanc
2. Address operator (&) • So where does my variable live? • By using the address operator (&) in front of any variable in your code, you can obtain its location in (logical) physical memory • If myVariable in my program is a charthen &myVariable is the address of that char • So now that we have the ability to obtain an address, we want to be able to store this address and manipulate it Prof S.P. Leblanc
3. Pointer variables • In C we have variables that can store computer addresses; they are pointer variables or pointers for short • Therefore if I use the & operator and obtain the address of a variable, I can assign this address to a pointer variable • A pointer is declared as follows: int* pointerToInt; //pointer to an int char* ptrToChar; //pointer to a char Prof S.P. Leblanc
3. Pointer variables • Notice the similarity between the way an array and a pointer are declared: int* pointerToInt; int tableOfInts[]; • So pointers, like arrays, are derived types. Prof S.P. Leblanc
3. Pointer variables – Memory Model Symbolic Memory … void main() { … int a = 145; int* p; // a pointer … p = &a; //take a’s address //and store it in //pointer p } 12500 a 145 p 46798 XXXX Memory Model Prof S.P. Leblanc
3. Pointer variables – Memory Model Symbolic Memory … void main() { … int a = 145; int* p; // a pointer … p = &a; //take a’s address //and store it in //pointer p } 12500 a 145 p 46798 12500 Memory Model Prof S.P. Leblanc
4. The indirection operator (*) Symbolic Memory … void main() { … int a=145; int* p; // a pointer … p = &a; *p = 99; //indirection } 12500 a 99 *p p 46798 12500 Prof S.P. Leblanc
4. The indirection operator (*) Symbolic … void main() { … int a = 145; int b = 0; int* p; //a pointer to int … p = &a; *p = 99; //indirection b = *p; //b is value of //variable pointed //to by p } Memory 12500 a 99 p 46798 57109 b 12500 99 Prof S.P. Leblanc
5. Initializing Pointers • A pointer, like any other variable in C, is not automatically initialized • It contains garbage • You should initialize all your pointers explicitly • This is good practice for all variables … • … it is critical for pointers! • You can initialize a pointer using a real address: int a; int* p; p = &a; Prof S.P. Leblanc
5. Initializing Pointers • Or use the constant NULL (in stddef.h) int* p = NULL; • If your pointer has a NULL value and you dereference it, you will get a run-time error • Dangling or wrongly initialized pointers are a major source of errors in C • One of the most effective thing you can do when working with pointers is to draw your pointer logic Prof S.P. Leblanc
Pointers - Advantages • Pointers allow us to pass the address of variables as parameters to functions • They are at the basis for dynamic memory allocation in C • Allow us to grow and shrink data structures if we do not know the size of data we will encounter • Effective use of memory – Excellent for small microcontrollers • Pointers allow for efficient manipulation of data in arrays Prof S.P. Leblanc
6. Working with pointers and addresses Symbolic Memory int a = 6; int b = 10; int* p = NULL; int* r = NULL; p = &a; r = p; //points at same //variable b = *r; 12500 a 6 32547 r 12500 57109 b p 46798 6 12500 Prof S.P. Leblanc
6. Working with pointers and addresses Symbolic Memory int a = 6; int b = 10; int* p=NULL; int* r=NULL; p = &a; r = p; // also points at a b = *r; //b = a p = &b; //now p points //to b *p = 8; //changes value //of b 12500 a 6 32547 r 12500 57109 b p 46798 8 57109 Prof S.P. Leblanc
6. Pointers - flexibility • All of these statements are equivalent: int a; int* p=&a; … //Following here, all a = a + 1; //the statements do a++; //the same thing. *p = *p + 1; (*p)++; //note the () a = *p + 1; Prof S.P. Leblanc
7. Pointer types • It is important to understand that a pointer does not only have a type; but that a pointer points to a variable of a specific type. • A pointer therefore takes on the attributes of the type it points to, as well as its own attributes • That is because you can dereference the pointer and apply the operations associated with the type being pointed to Prof S.P. Leblanc
7. Pointer types • So you cannot mix pointer types in statements: int* p; char* r; … r = p; //compile error • The only exception to this is the void pointer type (Later) Prof S.P. Leblanc
Quiz Time int a = 1; int b = 2; int* p, r; … p = &b; r = &a; *r = *p; //what are the values //of a and b? Prof S.P. Leblanc
Homework • This is a voluntary assignment. While not required, it is highly recommended. • If you submit your answers, they will be corrected and given back to you. int question, chapter=9; char book[]="Forouzan"; void main(void) { for(question=16; question<=20; question++) AnswerQuestion(book, chapter, question); } Prof S.P. Leblanc
Next Class • Strings Prof S.P. Leblanc