Introduction to Dynamic Memory Allocation Computer ScienceApplications Programming

  • Slides: 12
Download presentation
Introduction to Dynamic Memory Allocation Computer Science/Applications Programming in C M. RAVI KIRAN KUMAR

Introduction to Dynamic Memory Allocation Computer Science/Applications Programming in C M. RAVI KIRAN KUMAR MCA, M. Tech. Govt. Degree College , Gummalakshmipuram Email. Id: rkk. meduri@gmail. com

Objectives At the end of the session, students should be able to understand: -

Objectives At the end of the session, students should be able to understand: - How a program in execution resides in computer memory - Different segments in memory - Static memory allocation - Issues with static memory allocation - Impetus to dynamic memory allocation

Memory Layout of a Process A program in execution – a process Program =

Memory Layout of a Process A program in execution – a process Program = actual source code Process = running set of instructions residing in memory Cmd. Line Args. /Env. Vars Stack Heap Uninitialized data (bss) Initialized data Instructions/Text Stack generally grows from top to bottom and Heap grows from bottom up All values initialized to zero Read from the C program executable

Different Segments - Text segment: - Executable instructions; placed at the bottom; - Read-only;

Different Segments - Text segment: - Executable instructions; placed at the bottom; - Read-only; shared across multiple instances; - Initialized segment: - Global and static variables that are initialized; - Divided into read-only and read-write areas. - Uninitialized segment: - Global and static variables that are initialized to zero or that do not have explicit initialization; Called as “block started by symbol (bss)”.

Different Segments – Contd. - Stack segment: - All local variables, function calls and

Different Segments – Contd. - Stack segment: - All local variables, function calls and parameters are stored; - It grows from top to bottom; - Static memory allocation - Heap segment: - Takes care of dynamic memory allocation; - It grows from bottom to top; § Both stack and heap grow in opposite directions. § When they meet, memory error occurs.

Example Analyze the following code in Linux:

Example Analyze the following code in Linux:

A Sample Memory Layout Command Line Arguments / Environment Variables main(): *c func(): i

A Sample Memory Layout Command Line Arguments / Environment Variables main(): *c func(): i Stack Segment Heap Segment *c = malloc (10); fglobal = 0. 00; slong = 0; global = 10; s = 10; pi = 3. 14; Object Code (C Program) Uninitialized data(bss) Initialized data(RW/RO)

Static Memory Allocation - When you define a variable, memory is allocated statically. -

Static Memory Allocation - When you define a variable, memory is allocated statically. - It cannot be extended. - Sometimes, we end up wasting a lot of memory. - Intuitive to say that we are wasting a lot of space - It is better if we can allocate as much memory as needed. - How can we do that?

Dynamic Memory Allocation - All static memory allocations are done in stack segment -

Dynamic Memory Allocation - All static memory allocations are done in stack segment - It cannot be changed once allocated. - Let us take an array of 10 elements; we have filled in all the elements and we need more space. Can we extend it? - We have reserved space for 100 elements but we just 5 elements. Can we free up the unwanted memory? - Yes, we can but not in Stack Segment. - Heap comes to our rescue and can be used for allocation and release of memory

C Library - From the available block of memory, we need to allocate memory

C Library - From the available block of memory, we need to allocate memory according to multiple requests. - C library provides the needed framework - Brk(), sbrk() and mmap() system calls are used internally. Library functions: § malloc() § calloc() § realloc() § free()

Summary § A program while being executed resides in memory and consists of code,

Summary § A program while being executed resides in memory and consists of code, data, stack and heap segments. § All global and static variables – both initialized and uninitialized – are stored in data segment. § Functions and local variables are stored in stack segment. § Heap is used for allocating memory dynamically as needed. § Static memory allocation leads to memory wastage. § Dynamic memory allocation addresses this issue by letting us use memory only when needed and free it up when not needed. § C library is used for dynamic memory allocation.

References Text Books: - The C Programming Language – Brian W Kernighan and Dennis

References Text Books: - The C Programming Language – Brian W Kernighan and Dennis M Ritchie – Second Edition - Introduction to C Programming – Reema Thareja – Second Edition Unix Man Pages: - Man pages of malloc, calloc, realloc and free