Unformatted text preview:

1Chapter 16Chapter 16Why do we need Pointers?Call by Value vs. Call by Reference in detailImplementing ArraysBuffer Overflow / The “Stack Hack”2Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyA problem with parameter passing via A problem with parameter passing via stackstack Consider the following function that's designed toswap the values of its arguments. void Swap(int firstVal, int secondVal){int tempVal = firstVal;firstVal = secondVal;secondVal = tempVal;} int main () {int valueA = 3, valueB = 4;…Swap (valueA, valueB);…3Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyExecuting the Swap FunctionExecuting the Swap FunctionfirstValsecondVal valueB valueA 344 3R6before calltempVal firstValsecondVal valueB valueA 3434 3R6after callThese valueschanged......but thesedid not.Swapmain4Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyPointers and ArraysPointers and Arrays Functions such as the swap example need to be able access variables stored in memory locations outside of their own activation record – A function’s activation record defines its “scope”– We've seen examples of how to do this in Assembly. Pointer– Address of a variable in memory– Allows us to indirectly access variables in other words, we can talk about its address rather than its value Array (still a pointer!)– An area of allocated memory with values arranged sequentially– Expression a[4]refers to the 5th element of the array a The array variable is a pointer to the base of the array Base + offset Thus… the first element is 05Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyPointers in CPointers in C C lets us talk about and manipulate addresses as “pointer variables” But first, lets refresh the somewhat confusing bits. &: The “address-of” or “reference” operator– This operator does one thing.– It returns the address of the variable which follows#include <stdio.h> int main() { int x = 0; printf("Address of x "); printf("= 0x%p \n", &x); return 0; }Output: Address of x = 0x0065FDF46Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyPointers in CPointers in C How do we store addresses? Pointer variables! – Although all pointers in C are exactly the same type (address) they are also typed by the compiler so that the data to which they refer can be appropriately interpreted.– A pointer in C is always a pointer to a particular data type: int*, double*, char*, etc. Declaration– int *p; /* p is a pointer to an int */ Operators– *p -- returns the value pointed to by p (indirect address / dereference op) – &z -- returns the address of variable z (address-of operator) Important point of common confusion! – * means “a pointer variable” when used in a declaration– * means “access the information that this address points to” elsewhere– What does *3 mean?27Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyCheck for understandingCheck for understanding#include <stdio.h> int main() { int x = 12; int *ptr = &x; printf("Address of x:\t 0x%p\n", ptr); printf("Address of x:\t 0x%x\n", &x); printf("Address of ptr:\t 0x%x\n", &ptr); printf("Value of x:\t %d\n", *ptr); return 0; }Address of x: 0x0065FDF4 Address of x: 0x65fdf4 Address of ptr: 0x65fdf0 Value of x: 12 8Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyCheck for understandingCheck for understanding#include <stdio.h> int main() { int x[10] = {0,1,2,3,4,5,6,7,8,9}; printf("Address of x[0]:\t 0x%p\n", &x[0]); printf("Address of x:\t 0x%p\n", x); printf("Value of x[0]:\t %d\n", x[0]); printf("Value of x[0]:\t %d\n", *x); return 0; }Address of x[0]: 0x0065FDD0 Address of x: 0x0065FDD0 Value of x[0]: 0 Value of x[0]: 0 9Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyExampleExampleint i;int *ptr;i = 4;ptr = &i;*ptr = *ptr + 1;store the value 4 into the memory locationassociated with istore the address of i into the memory location associated with ptrread the contents of memoryat the address stored in ptrstore the result into memoryat the address stored in ptr10Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyExample: LCExample: LC--3 Code3 Code; i is 1st local (offset 0), ptr is 2nd (offset -1); i = 4;AND R0, R0, #0 ; clear R0ADD R0, R0, #4 ; put 4 in R0STR R0, R5, #0 ; store in i; ptr = &i;ADD R0, R5, #0 ; R0 = R5 + 0 (addr of i)STR R0, R5, #-1 ; store in ptr; *ptr = *ptr + 1;LDR R0, R5, #-1 ; R0 = ptrLDR R1, R0, #0 ; load contents (*ptr)ADD R1, R1, #1 ; add oneSTR R1, R0, #0 ; store result where R0 pointsNameType OffsetScopeiptrintInt*0-1mainmainptrifppcretxEFFC4xxxxxxxxxxxxxF000R6R511Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyCall by referenceCall by reference Passing a pointer as an argument allows the function to read/change memory outside its activation record.– But not the pointer itself!– If you wanted to change the pointer itself, what would you need to do? void NewSwap(int *firstVal, int *secondVal){int tempVal = *firstVal;*firstVal = *secondVal;*secondVal = tempVal;}Arguments areinteger pointers.Caller passes addressesof variables that it wantsfunction to change.12Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyPassing Pointers to a FunctionPassing Pointers to a Functionmain() wants to swap the values of valueA and valueBNewSwap(&valueA, &valueB);LC-3 Code for main:ADD R0, R5, #-1 ; addr of valueBADD R6, R6, #-1 ; pushSTR R0, R6, #0ADD R0, R5, #0 ; addr of valueAADD R6, R6, #-1 ; pushSTR R0, R6, #0firstValsecondValvalueBvalueAxEFFAxEFF94 3xEFFDR6R5313Wright State University, College of EngineeringDr. Doo m, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyCode Using PointersCode Using


View Full Document

Wright CEG 320 - Pointers and Arrays

Download Pointers and Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Pointers and Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Pointers and Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?