Week 7 - Wednesday. What did we talk about last time? scanf() Memory allocation malloc() free()
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and...
-
Upload
chloe-baker -
Category
Documents
-
view
215 -
download
1
Transcript of 1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and...
1
• Dynamic Memory Allocation– The need– malloc/free– Memory Leaks– Dangling Pointers and Garbage Collection
Today’s Material
2
• C’s 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 A[10];
– Structure and Union sizes are also fixed
• Often the actual size of an array is not known until the execution time!
Dynamic Memory Allocation: Why?
3
• 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
Dynamic Memory Allocation: Why?
4
• 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 A[N];
• 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
Dynamic Memory Allocation: Why?
5
• C library provides 2 functions that perform dynamic memory allocation and de-allocation
Dynamic Memory Allocation Functions
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);if (p == NULL){ printf(“Out of memory\n”); exit(1);} /* end-if */
p[0] = ‘a’;p[1] = ‘b’;p[2] = ‘\0’;printf(“p=<%s>\n”, p); /* Will print p=<ab> */
50 bytes longp
• p points to a 50 byte long contiguous memory chunk• How you use this memory is application dependant
7
Example (2)
char *pi = NULL;
/* Allocate space for 25 integers */pi = (int *)malloc(sizeof(int)*25);
pi[0] = 5;pi[1] = 2;
free(pi); /* Deallocate space */
• What if we want to allocate space for say 25 integers?
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(10*sizeof(int));pi[0] = 2;..../* Allocate space for 20 ints without deallocating * space for the previous 10 ints */pi = malloc(20*sizeof(int));pi[0] = 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
char *p, *q;
p = (char *)malloc(10);q = p;p[0] = ‘A’;q[1] = ‘B’;....free(p);q[0] = ‘X’; /* Wrong! */
• It is illegal to use memory that has been freed • Consider the following program
10 bytes longp
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 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