CS 354 - Machine Organization Friday, September 23, 2016: Homework hw2 (1.5%) Due 10 PM Tuesday, September 27th

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CS 354 - Machine Organization

Friday, September 23, 2016

Homework hw2 (1.5%) due 10 pm Tuesday, September 27th


Project p2 (6%) due 10 pm Friday, October 7th

Last Time
Pointers to structs
Strings and string.h.
Standard & String I/O and stdio.h
Today
File I/O and stdio.h (from last time)
Where Do I Live?
C Memory Model
Address Space Layout
Next Time
Read: B&O 9.9.3 - 9.9.4
Dynamic Memory

Copyright 2016 Jim Skrentny

CS 354 (F16): L8 - 1

Where do I live?
#include <stdio.h>
#include <stdlib.h>
int gus = 14;
int guy;
int madison(int pam) {
static int max = 0;
int meg[] = {22,44,88};
int *mel = &pam;
max = gus--;
return max + meg[1] + *mel;
}
int *austin(int *pat){
static int amy = 33;
int *ari = malloc(sizeof(int)*44);
gus--;
*ari = *pat;
return ari;
}
int main(int argc, char *argv[]) {
int vic = {33,66,99};
int *wes = malloc(sizeof(int));
*wes = 55;
guy = 66;
free(wes);
wes = vic;
wes[1] = madison(guy);
wes = austin(&gus);
free(wes);
printf("Where do I live?");
return 0;
}

Copyright 2016 Jim Skrentny

CS 354 (F16): L8 - 2

C Memory Model
1. Text Segment (AKA Code Segment)
Contains: programs executable instructions from its object file

2. Initialized Data Segment


Memory allocation with a specified size, type, and non-zero initial value.
Lifetime: entire program executiton
Initialized Read-Only Area - Contains:
Initialized Read-Write Area - Contains:
3. Uninitialized Data Segment (AKA BSS Segment)
Memory allocation with a specified size, type and uninitialized or initialized to zero.
Lifetime: entire program executiton
Contains:

4. Heap
Memory allocation requested during execution.
Lifetime:

Contains:

5. Stack (AKA Call Stack)


Memory automatically allocated and freed as functions are executed.
Lifetime:
Contains:
LIFO:

Stack Frame:

Copyright 2016 Jim Skrentny

CS 354 (F16): L8 - 3

Virtual Address Space for a Process


0xFFFFFFFF

32-bit Processor

11111111111111111111111111111111

OS Kernal

32-bit addresses:

0xC0000000/0x80000000

11111110110010010101001000010000

0xFEC95210
each address accesses 1 byte
max addressable bytes:
232 = 4,294,967,296 = 4GB

5. Stack

Unused

4. Heap

3. Uninitialized Data
bss
2. Initialized Data
data
1. Program Code
text

0x08048000
00001000000001001000000000000000

OS/C
Libraries/Misc
0x00000000
00000000000000000000000000000000

Copyright 2016 Jim Skrentny

CS 354 (F16): L8 - 4

You might also like