Dynamic Memory Allocation - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Dynamic Memory Allocation

Description:

C's data structures have fixed size known at compile time. char 1 byte. short 2 bytes ... 1 million integers, but use only the first 100, wasting the rest ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 12
Provided by: cengAna
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Memory Allocation


1
Todays Material
  • Dynamic Memory Allocation
  • The need
  • malloc/free
  • Memory Leaks
  • Dangling Pointers and Garbage Collection

2
Dynamic Memory Allocation Why?
  • Cs data structures have fixed size known at
    compile time
  • char 1 byte
  • short 2 bytes
  • int 4 bytes
  • float 4 bytes
  • double 8 bytes
  • Array size must be known at compile time and is
    fixed
  • int A10
  • Structure and Union sizes are also fixed
  • Often the actual size of an array is not known
    until the execution time!

3
Dynamic Memory Allocation Why?
  • Consider a simple program that takes some number
    of integers and performs some operation on them
  • The question that arises is the size of the array
    that will store the integers

4
Dynamic Memory Allocation Why?
  • The usual approach is to declare an array that is
    as big as it ever will need to be
  • define N 1000000 / at most 1 million numbers /
  • int AN
  • Adv Simple
  • Disadvantages
  • Even if we manipulate only 100 numbers, we still
    reserve space for 1 million integers, but use
    only the first 100, wasting the rest
  • Cannot handle input larger than 1 million
  • Solution
  • Dynamically allocate as much space as needed
    during execution

5
Dynamic Memory Allocation Functions
  • C library provides 2 functions that perform
    dynamic memory allocation and de-allocation

void malloc(int size) / Allocates size many
bytes of contiguous memory and return a
pointer to the beginning of the memory
chunk. Returns NULL if the memory cannot be
allocated / void free(void pointer) /
Deallocates memory previously allocated by
malloc pointed to by pointer /
6
Example (1)
char p NULL p (char )malloc(50) / Type
casting must be applied to the return value
/ if (p NULL) printf("Out of memory\n")
exit(1) p0 'a' p1 'b' p2
'\0' printf("pltsgt\n", p) / Will print pltabgt
/
p
50 bytes long
  • p points to a 50 byte long contiguous memory
    chunk
  • How you use this memory is application dependant

7
Example (2)
  • What if we want to allocate space for say 25
    integers?

int pi NULL / Allocate space for 25
integers / pi (int )malloc(25
sizeof(int)) / Note that the size of the
allocated space is not 25 bytes but 254100
bytes because an integer occupies 4 bytes!!!
/ pi0 5 pi1 2 free(pi) /
Deallocate space /
8
Memory Leaks
  • It is your responsibility to deallocate memory
    chunks when you are done with them
  • If you forget to deallocate the space, it will
    still be part of your address space, but you
    cannot access it
  • This is called a memory leak

9
Memory Leaks Example
/ Allocate space for 10 ints / pi
malloc(10sizeof(int)) pi0 2 .. .. /
Allocate space for 20 ints without deallocating
space for the previous 10 ints / pi
malloc(20sizeof(int)) pi0 3 / Notice
that we lost the handle to the previous memory
chunk. That space has leaked out of the
program. If your program runs long enough,
leaking memory like this continuously, it will
be out of memory. /
10
Dangling Pointer Problem
  • It is illegal to use memory that has been freed
  • Consider the following program

char p, q p (char )malloc(10) q p p0
'A' q1 'B' .. .. free(p) q0 'X' /
Wrong! /
p
10 bytes long
q
  • At this point the memory chunk pointed to by
    either p or q is invalid. It has been freed and
    cannot be accessed
  • p q point to storage that is no longer
    allocated and they are called dangling pointers

11
Dangling Pointers (cont)
  • To avoid memory leaks and dangling pointers,
    programming languages such as Java, C handle
    dynamic memory deallocation automatically
  • This is called garbage collection
  • The runtime environment of Java and C has
    automatic garbage collectors
  • Thus programmers do not have to deallocate memory
  • The disadvantage of this approach is speed
  • Java, C is slower compared to C, C
  • safety ?versus? speed tradeoff
Write a Comment
User Comments (0)
About PowerShow.com