Heap-stack Diagram Sanzheng Qiao McMaster University November, - - PowerPoint PPT Presentation

heap stack diagram
SMART_READER_LITE
LIVE PREVIEW

Heap-stack Diagram Sanzheng Qiao McMaster University November, - - PowerPoint PPT Presentation

Sizes of Objects Generating Heap-stack Diagrams Example Heap-stack Diagram Sanzheng Qiao McMaster University November, 2011 Sizes of Objects Generating Heap-stack Diagrams Example Outline 1 Sizes of Objects 2 Generating Heap-stack


slide-1
SLIDE 1

Sizes of Objects Generating Heap-stack Diagrams Example

Heap-stack Diagram

Sanzheng Qiao

McMaster University

November, 2011

slide-2
SLIDE 2

Sizes of Objects Generating Heap-stack Diagrams Example

Outline

1

Sizes of Objects

2

Generating Heap-stack Diagrams

3

Example

slide-3
SLIDE 3

Sizes of Objects Generating Heap-stack Diagrams Example

Sizes of objects

1 byte char, bool 2 bytes short 4 bytes int, float 8 bytes long, double 16 bytes long double 1 byte = 8 bits

slide-4
SLIDE 4

Sizes of Objects Generating Heap-stack Diagrams Example

Sizes of objects

enumerated type (enum): 4 bytes structure type (struct): enough space for all individual fields, no overlap array: enough space for all elements, assigned consecutive locations pointer: 4 bytes Memory is byte addressable.

slide-5
SLIDE 5

Sizes of Objects Generating Heap-stack Diagrams Example

Outline

1

Sizes of Objects

2

Generating Heap-stack Diagrams

3

Example

slide-6
SLIDE 6

Sizes of Objects Generating Heap-stack Diagrams Example

Generating heap-stack diagrams

Start with an empty diagram, heap and stack side by side. Heap expands towards larger memory addresses, starting say 1000, grows downward on page, allocate heap memory when new operator appears Stack builds towards smaller memory addresses, starting say FFFF, grows upward on page

slide-7
SLIDE 7

Sizes of Objects Generating Heap-stack Diagrams Example

Generating heap-stack diagrams

Manually trace the program, allocating memory as you go Add a new stack frame for each function call

Use a shaded block as overhead to separate stack frames Spaces for all local variables (arguments, variables declared in the body, loop index), label the spaces Initialize the arguments Pop the stack frame when the function returns

slide-8
SLIDE 8

Sizes of Objects Generating Heap-stack Diagrams Example

Outline

1

Sizes of Objects

2

Generating Heap-stack Diagrams

3

Example

slide-9
SLIDE 9

Sizes of Objects Generating Heap-stack Diagrams Example

Example

int main() { pointT pt; pt.x = 1; pt.y = 2; int *array = new int[3]; Nonsense(array, pt); return 0; } void Nonsense(int list[], pointT &ptr) { list[1] = ptr->x; }

heap stack

slide-10
SLIDE 10

Sizes of Objects Generating Heap-stack Diagrams Example

Example

int main() { pointT pt; pt.x = 1; pt.y = 2; <----- diagram int *array = new int[3]; Nonsense(array, pt); return 0; } void Nonsense(int list[], pointT &ptr) { list[1] = ptr->x; }

heap stack FFF4 FFF8 pt 1 2

slide-11
SLIDE 11

Sizes of Objects Generating Heap-stack Diagrams Example

Example

int main() { pointT pt; pt.x = 1; pt.y = 2; int *array = new int[3]; <----- diagram Nonsense(array, pt); return 0; } void Nonsense(int list[], pointT &ptr) { list[1] = ptr->x; }

heap stack FFF0 FFF4 FFF8 array pt 1000 1004 1000 1 2

slide-12
SLIDE 12

Sizes of Objects Generating Heap-stack Diagrams Example

Example

int main() { pointT pt; pt.x = 1; pt.y = 2; int *array = new int[3]; Nonsense(array, pt); return 0; } void Nonsense(int list[], pointT &ptr) { list[1] = ptr->x; <----- diagram } heap stack FFF0 FFF4 FFF8 FFE8 FFE4 ptr list array pt 1000 1004 1 1000 1 2 1000 FFF4