210 likes | 230 Views
Stack. Heap. Fixed. 3 Types of Memory Allocation. 0xf*******. local variables. allocated with descendant addresses. pointer’s content. allocated with ascendant addresses. global or static variables that are determined when the program is loaded. Usually < 0x10000000.
E N D
Stack Heap Fixed 3 Types of Memory Allocation 0xf******* local variables allocated with descendant addresses pointer’s content allocated with ascendant addresses global or static variables that are determined when the program is loaded Usually < 0x10000000
8.3 Pointer Operators • Address operator (&) • Returns memory address of its operand • Example • int y = 5;int *yPtr;yPtr = &y;assigns the address of variable y to pointer variable yPtr • Variable yPtr “points to” y • yPtr indirectly references variable y’s value
8.3 Pointer Operators (Cont.) • * operator • Also called indirection operator or dereferencing operator • Returns synonym for the object its operand points to • *yPtr returns y (because yPtr points to y) • Dereferenced pointer is an lvalue *yptr = 9; • * and & are inverses of each other • Will “cancel one another out” when applied consecutively in either order
void f1(int a) { a = 20; } void f2(int& a) { a = 30; } void f3(int* p) { *p = 40; } void f4(int* p) { p = new int(50); } void f5(int* & p) { p = new int(60); } main() { int a = 10; int* p = &a; int a1,a2,a3,a4,a5; f1(a); a1 = a; f2(a); a2 = a; f3(p); a3 = *p; f4(p); a4 = *p; f5(p); a5 = *p; } Passed by Object, Pointer, and Reference [Rule of thumb] Making an ‘=’ (i.e. copy) from the passed variable in the caller, to the parameter of the called function. What are the values of a1, a2, a3, a4, and a5 at the end?