Memory Layout C and Data Structures Baojian Hua [email protected].

30
Memory Layout C and Data Structures Baojian Hua [email protected]
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    235
  • download

    1

Transcript of Memory Layout C and Data Structures Baojian Hua [email protected].

Memory Layout

C and Data StructuresBaojian Hua

[email protected]

Goals of Today’s Lecture

Behind the scenes of running a program Code, executable, and process

Memory layout for UNIX processes, and relationship to C

Explicit memory management in C malloc: allocate memory from the heap free: deallocate memory from the heap

From C Code to Process C source files

.c; .h Binary files

.o Executables

a.out Process

Managed by OS

binary files

C source code

process

compiling

running

executable

linking

Main Memory

CPU

Memory Disk

Network

Video

Audio

Data Bus

shared by all processes

Virtual Memory

Continuous memory space for all process each with its physical

space pretends you the same

virtual space

0xffffffff

0

Organization of Virtual Memory: .text

Program code and constant binary form loaded libraries

0xffffffff

0text

Organization of Virtual Memory: .text

Program code and constant binary form loaded libraries known as “text”

segment space calculated at

compile-time 0xffffffff

0text

Organization of Virtual Memory: .data

Data: initialized global data in the program Ex: int size = 100;

BSS: un-initialized global data in the program Ex: int length;

0xffffffff

0text

data

bss

Organization of Virtual Memory: heap

Heap: dynamically-allocated spaces Ex: malloc, free OS knows nothing about it

space content

dynamically grows as program runs

0xffffffff

0text

data

bss

heap

Organization of Virtual Memory: stack

Stack: local variables in functions we’ll discuss stack soon support function

call/return and recursive functions

grow to low address

0xffffffff

0text

data

bss

heap

stack

Summary text: program text data: initialized globals & stat

ic data bss: un-initialized globals & st

atic data heap: dynamically managed

memory stack: function local variables

0xffffffff

0text

data

bss

heap

stack

Examplechar *string = “hello”;

int iSize;

char *f(int x){ char *p;

iSize = 8; p = malloc (iSize); return p;}

0xffffffff

0text

data

bss

heap

stack

Examplechar *string = “hello”;

int iSize;

char *f (int x){ char *p;

iSize = 8; p = malloc (iSize); return p;}

0xffffffff

0text

data

bss

heap

stack

Variable Lifetime text:

program startup program finish

data, bss: program startup program finish

heap: dynamically allocated de-allocated (free)

stack: function call function return 0xffffffff

0text

data

bss

heap

stack

Examplechar *string = “hello”;

int iSize;

char *f (int x){ char* p;

iSize = 8; p = malloc (iSize); return p;}

0xffffffff

0text

data

bss

heap

stack

program startup

when f() is called

live after allocation; till free() or program finish

Variable Initialization text:

readonly data

on program startup bss:

un-initialized (though some systems initialize with 0)

heap: un-initialized

stack: un-initialized 0xffffffff

0text

data

bss

heap

stack

Explicit Memory Management

Heap management in C is explicit “malloc” to request a region “free” to recycle a region

It’s the programmers’ responsibility to make sure that such a sequence of action is safe

Exampleint main(){ int *p;

p = malloc (4); *p = 99;

return 0;}

0xffffffff

0text

data

bss

heap

stack

p

Exampleint main(){ int *p;

p = malloc (4); *p = 99;

return 0;}

0xffffffff

0text

data

bss

heap

stack

#@%*&

p

Exampleint main(){ int *p;

p = malloc (4); *p = 99;

return 0;}

0xffffffff

0text

data

bss

heap

stack

99

p

Exampleint main(){ int *p;

p = malloc (4); *p = 99;

int *q;

q = p; // alias

return 0;}

0xffffffff

0text

data

bss

heap

stack

99

pq

Exampleint main(){ int *p;

p = malloc (4); *p = 99;

int *q;

q = p;

printf (“%d\n”, *q);

return 0;}

0xffffffff

0text

data

bss

heap

stack

99

pq

Exampleint main(){ int *p;

p = malloc (4); *p = 99;

int *q;

q = p;

printf (“%d\n”, *q);

free (q);

return 0;}

0xffffffff

0text

data

bss

heap

stack

pq

Dangling Pointer Referenceint main(){ int *p, *q;

q = p = (int *) malloc (4); free (q); *p;

return 0;}

Dangling Dereferenceint main(){ int *p, *q;

q = p = (int *) malloc (4); free (q); *p;

return 0;}

0xffffffff

0text

data

bss

heap

stack

#@%*&

pq

Dangling Dereferenceint main(){ int *p, *q;

q = p = (int *) malloc (4); free (q); *p;

return 0;}

0xffffffff

0text

data

bss

heap

stack

pq

Dangling Dereferenceint main(){ int *p, *q;

q = p = (int *) malloc (4); free (q); *p; // no this memory!!!

return 0;}

0xffffffff

0text

data

bss

heap

stack

pq

Memory Leakint main(){ int *p; p = (int *) malloc (4);

// make the above space unreachable p = (int *) malloc (4);

// even worse… while (1) p = malloc (4); return 0;}

Memory Leakvoid f ();

void f (){ int *p; p = malloc (4); return;}

int main (){ f (); return 0;}

Summary Dangling pointers and memory

leak are evil sources of bugs: hard to debug

may fire after a long time of run may far from the bug point

hard to prevent especially by using the static methods

Part of the reasons for the popularity of garbage collection