Memory Management in UNIX: Understanding Text, Data, BSS, Heap, and Stack - Prof. Mirela D, Study notes of Operating Systems

An in-depth explanation of memory management in unix systems. It covers the concepts of code, executable, and processes, and discusses the memory layout for unix processes, including the text, data, bss, heap, and stack segments. The document also explains the use of c functions for memory management, such as malloc() and free(), and provides examples of memory allocation and deallocation.

Typology: Study notes

Pre 2010

Uploaded on 02/25/2010

koofers-user-47b
koofers-user-47b 🇺🇸

10 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
Memory Allocation
Slides by Jennifer Rexford from Princeton University, slightly modified by Mirela Damian.
2
Lecture Goals
Behind the scenes of running a program
oCode, executable, and process
oMain memory vs. virtual memory
Memory layout for UNIX processes, and relationship to C
oText: code and constant data
oData: initialized global and static variables
oBSS: uninitialized global and static variables
oHeap: dynamic memory
oStack: local variables
C functions for memory management
omalloc: allocate memory from the heap
ofree: deallocate memory from the heap
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Memory Management in UNIX: Understanding Text, Data, BSS, Heap, and Stack - Prof. Mirela D and more Study notes Operating Systems in PDF only on Docsity!

1

Memory Allocation

Slides by Jennifer Rexford from Princeton University, slightly modified by Mirela Damian.

2

Lecture Goals

  • Behind the scenes of running a program o Code, executable, and process o Main memory vs. virtual memory
  • Memory layout for UNIX processes, and relationship to C o Text: code and constant data o Data: initialized global and static variables o BSS: uninitialized global and static variables o Heap: dynamic memory o Stack: local variables
  • C functions for memory management o malloc: allocate memory from the heap o free: deallocate memory from the heap

3

Code vs. Executable vs. Process

  • C source code o C statements organized into functions o Stored as a collection of files (.c and .h)
  • Executable module o Binary image generated by compiler o Stored as a file (e.g., a.out )
  • Process o Instance of a program that is executing - With its own address space in memory - With its own id and execution state o Managed by the operating system

executable

C source code

process

compiling

running

4

Main Memory on a Computer

  • What is main memory?

o Storage for variables, data, code, etc.

o May be shared among many processes

CPU

Memory

Disk

Network

Video

Audio

Data Bus

7

What to Store: “Static” Data

  • Variables that exist for the entire program o Global variables, and “static” local variables o Amount of space required is known in advance
  • Data: initialized in the code o Initial value specified by the programmer - E.g., “int x = 97;” o Memory is initialized with this value
  • BSS: not initialized in the code o Initial value not specified - E.g., “int x;” o All memory initialized to 0 (on most OS’s) o BSS stands for “Block Started by Symbol”

0

0xffffffff

Text

Data

BSS

8

What to Store: Dynamic Memory

  • Memory allocated while program is running o E.g., allocated using the malloc() function - And deallocated using the free() function
  • OS knows nothing in advance o Doesn’t know the amount of space o Doesn’t know the contents
  • So, need to allow room to grow o Known as the “heap” o Detailed example in a few slides o More in programming assignment #

0

0xffffffff

Text

Data

BSS

Heap

9

What to Store: Temporary Variables

  • Temporary memory during lifetime of a function or block o Storage for function parameters and local variables
  • Need to support nested function calls o One function calls another, and so on o Store the variables of calling function o Know where to return when done
  • So, must allow room to grow o Known as the “stack” o Push on the stack as new function is called o Pop off the stack as the function ends
  • Detailed example later on

0

0xffffffff

Text

Data

BSS

Heap

Stack

10

Memory Layout: Summary

  • Text: code, constant data
  • Data: initialized global & static variables
  • BSS: uninitialized global & static variables
  • Heap: dynamic memory
  • Stack: local variables

0

0xffffffff

Text

Data

BSS

Heap

Stack

13

Memory Layout Example: Data

char* string = “hello”; int iSize;

char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; }

Text

Data

BSS

Stack

Heap

14

Memory Layout Example: BSS

char* string = “hello”; int iSize;

char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; }

Text

Data

BSS

Stack

Heap

15

Memory Layout Example: Heap

char* string = “hello”; int iSize;

char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; }

Text

Data

BSS

Stack

Heap

16

Memory Layout Example: Stack

char* string = “hello”; int iSize;

char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; }

Text

Data

BSS

Stack

Heap

19

Memory Deallocation Example

char* string = “hello”;

int iSize;

char* f(void)

char* p;

iSize = 8;

p = malloc(iSize);

return p;

Available till termination

Deallocate on return from f

Deallocate on free()

Available till termination

20

Memory Initialization

  • Local variables have undefined values int count;
  • Memory allocated by malloc() has undefined values char* p = (char *) malloc(8);
  • Must explicitly initialize if you want a particular initial value

int count = 0; p[0] = ’\0’;

  • Global and static variables are initialized to 0 by default static int count = 0; is the same as static int count; (^) It is bad style to depend on this

21

Heap: Dynamic Memory

#include <stdlib.h> void *malloc(size_t size); void free(void *ptr); 0

0xffffffff

Text Data BSS

Stack

Heap

Heap

char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5);

p

22

Heap: Dynamic Memory

#include <stdlib.h> void *malloc(size_t size); void free(void *ptr); 0

0xffffffff

Text Data BSS

Stack

Heap

Heap

char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5);

p p

25

Heap: Dynamic Memory

#include <stdlib.h> void *malloc(size_t size); void free(void *ptr); 0

0xffffffff

Text Data BSS

Stack

Heap

Heap

char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5);

p p p

p

26

Heap: Dynamic Memory

#include <stdlib.h> void *malloc(size_t size); void free(void *ptr); 0

0xffffffff

Text Data BSS

Stack

Heap

Heap

char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5);

p p p

p

27

Heap: Dynamic Memory

#include <stdlib.h> void *malloc(size_t size); void free(void *ptr); 0

0xffffffff

Text Data BSS

Stack

Heap

Heap

char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5);

p p5, p p

p

28

Heap: Dynamic Memory

#include <stdlib.h> void *malloc(size_t size); void free(void *ptr); 0

0xffffffff

Text Data BSS

Stack

Heap

Heap

char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5);

p p5, p p

p

31

Using Malloc and Free

  • Types o void*: generic pointer to any type (can be converted to other types) o size_t: unsigned integer type returned by sizeof()
  • void* malloc(size_t size) o Returns a pointer to space of size size o … or NULL if the request cannot be satisfied o E.g., int* x = (int *) malloc(sizeof(int));
  • void* calloc(size_t nobj, size_t size) o Returns a pointer to space for array of nobj objects of size size o … or NULL if the request cannot be satisfied o Bytes are initialized to 0
  • void free(void* p) o Deallocate the space pointed to by the pointer p o Pointer p must be pointer to space previously allocated o Do nothing if p is NULL

32

Using realloc and (never) alloca

  • void* realloc(void* ptr, size_t size) o “Grows” the allocated buffer o Moves/copies the data if old space insufficient o … or NULL if the request cannot be satisfied
  • void* alloca(size_t size) o Not guaranteed to exist (not in any official standard) o Allocates space on local stack frame o Space automatically freed when function exits o Particularly useful for following: int calc(int numItems) { int items[numItems]; int *items = alloca(numItems * sizeof(int)); }

33

Examples Motivating Realloc()

  • Example programming tasks o Read text consisting of multiple lines (ending in ‘\n’) o Print the lines in alphabetical order
  • Suppose you don’t know the maximum line size in advance o Could pick a maximum line size in advance o E.g., #define MAXCHAR 200 o But, what if you need to read and store a longer line?
  • And you don’t know the number of lines in advance o Could pick a maximum number of lines in advance o E.g., #define MAXLINE 10000 o But, what if the input has more lines than this?
  • Better to (re)allocate more memory as you go along

34

Printing Chars in Reverse Order

#define MAXCHAR 1000

int main(void) { char a[MAXCHAR]; int i, c; for (i=0; i<MAXCHAR && ((c=getchar()) != EOF); i++) a[i] = c;

while (i > 0) putchar(a[--i]); putchar('\n'); }

37

Avoid Dangling Pointers

  • Dangling pointers point to data that’s not there anymore

char *f(void)

char p[8];

return p;

int main(void) {

char *res = f();

38

Debugging Malloc Problems

  • Symptom: “random” failures, especially on call return o Corrupted the stack frame return info
  • Symptom: calls to malloc/free fail o Corrupted the malloc bookkeeping data
  • Symptom: program magically works if printf inserted o Corrupted storage space in stack frame
  • “Debugging” mallocs exist o Doing “man malloc” on Linux reveals MALLOC_CHECK_ o Searching “debug malloc” yields dmalloc, other libraries o Larger problems: valgrind, electric fence, etc.

39

Summary

  • Five types of memory for variables o Text: code, constant data (constant data in rodata on hats) o Data: initialized global & static variables o BSS: uninitialized global & static variables o Heap: dynamic memory o Stack: local variables
  • Important to understand differences between o Allocation: space allocated o Initialization: initial value, if any o Deallocation: space reclaimed
  • Understanding memory allocation is important o Make efficient use of memory o Avoid “memory leaks” from dangling pointers