1 / 21

3 Types of Memory Allocation

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.

wgaffney
Download Presentation

3 Types of Memory Allocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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?

More Related