0% found this document useful (0 votes)
26 views12 pages

Heap-Stack Diagram: Sanzheng Qiao

The document discusses the sizes of different data types in memory and how to generate heap-stack diagrams to visualize memory allocation. It outlines how to generate heap-stack diagrams by manually tracing a program's execution and allocating memory on the heap and stack as needed. An example program is provided and its memory allocation is visualized through a step-by-step heap-stack diagram.

Uploaded by

Isoken Efionayi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views12 pages

Heap-Stack Diagram: Sanzheng Qiao

The document discusses the sizes of different data types in memory and how to generate heap-stack diagrams to visualize memory allocation. It outlines how to generate heap-stack diagrams by manually tracing a program's execution and allocating memory on the heap and stack as needed. An example program is provided and its memory allocation is visualized through a step-by-step heap-stack diagram.

Uploaded by

Isoken Efionayi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Sizes of Objects

Generating Heap-stack Diagrams

Heap-stack Diagram
Sanzheng Qiao
McMaster University

November, 2011

Example

Sizes of Objects

Generating Heap-stack Diagrams

Outline

Sizes of Objects

Generating Heap-stack Diagrams

Example

Example

Sizes of Objects

Generating Heap-stack Diagrams

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

Example

Sizes of Objects

Generating Heap-stack Diagrams

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.

Example

Sizes of Objects

Generating Heap-stack Diagrams

Outline

Sizes of Objects

Generating Heap-stack Diagrams

Example

Example

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

Sizes of Objects

Generating Heap-stack Diagrams

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

Example

Sizes of Objects

Generating Heap-stack Diagrams

Outline

Sizes of Objects

Generating Heap-stack Diagrams

Example

Example

Sizes of Objects

Generating Heap-stack Diagrams

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

Example

Sizes of Objects

Generating Heap-stack Diagrams

Example

Example
int main()
pointT
pt.x =
pt.y =

{
pt;
1;
2;
<----- diagram

int *array = new int[3];


Nonsense(array, pt);
return 0;
}
void Nonsense(int list[], pointT &ptr) {
list[1] = ptr->x;
}

heap

stack

pt

1
2

FFF4
FFF8

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
1000
1004
array

1000

FFF0

pt

FFF4
FFF8

Sizes of Objects

Generating Heap-stack Diagrams

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
1000

ptr

FFF4

FFE4

1004

list

1000

FFE8

array

1000

FFF0

pt

FFF4
FFF8

Example

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy