Lec12_Memory Management (1) (2)
Lec12_Memory Management (1) (2)
programmin
g
Lecture 12: Memory management
Dr. Ala’a Al-Habashna
Where to start?
Memory
allocation
• There are essentially two types of memory
allocation:
1. Static memory allocation
2. Dynamic memory allocation
Static Memory
Allocation
• Static – Done by the compiler automatically (implicitly).
1. Global variables or objects -- memory is allocated at the start of the program, and freed
when program exits; alive throughout program execution.
• Can be accessed anywhere in the program.
2. Local variables (inside a routine) – memory is allocated when the routine starts and freed
when the routine returns.
• A local variable cannot be accessed from another routine.
• Allocation and free are done implicitly.
• No need to explicitly manage memory is nice (easy to work with) but has limitations!
• Using static allocation, the array or variable size must be fixed.
• Once allocated, memory cannot be resized.
• Dynamic – Done explicitly by programmer:
Dynamic • Programmer explicitly requests the system to allocate
memory and return starting address of memory
Memory allocated.
• This address can be used by the programmer to
Allocation access the allocated memory.
• When done using memory, it must be explicitly freed.
• Function prototype:
void *malloc(size_t size)
malloc( • Description:
• reserves in memory the number of bytes specified in
size.
n
• free() function.
• program termination.
Example
Dynamic
double *a;
a = (double *) malloc(sizeof(double));
*a = 3.14;
printf("%lf\n", *a);
free(a);
The output is:
3.14
What do we call this?
double *a;
a = (double *) malloc(sizeof(double));
a = (double *) malloc(sizeof(double));
Memory leak
• Why?
• You can not reuse memory that’s allocated.
• No one else will clean your mess
• Function prototype:
void free(void *ptr)
• Description:
• Releases the memory pointed to by ptr.
• Function prototype:
void *calloc(size_t nitems, size_t
size)
• Description:
• Contiguous allocation
calloc() • Reserves in memory n items, each the number
of bytes specified in size.
Function • Returns the start of the new block of reserved
memory.
• Example:
double *a;
a=(double *) calloc(70, sizeof(double));
• Dynamically allocated memory can be accessed:
• Through pointers
• Using array notation
Accessing
Dynamically • Example:
int *A;
Allocated A=(int *) calloc(5, sizeof(int));
A[0]=8;
Memory *(A+2)=3; //A[2] = 3
A[3]=9;
• What is malloc()?
• The malloc is also known as the memory allocation function
• malloc() dynamically allocates a large block of memory with a specific size
Malloc() VS • It returns a void type pointer and is cast into any form
E.g.
int *arr1 = (int *)malloc(5 * sizeof(int));
int *arr2 = (int *)calloc(5, sizeof(int));
• Write program to
• Declare an array of type char and length 100 (call it name).
Exercise • Use malloc to allocate memory for a string (char of size 200)
pointed to by a pointer (call it description).
• Assign the string “DB programming” to name.
• Assign the string “Programming” to the variable pointed to by
description.
• Print the both strings.
• Free the memory allocation for description.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char name[100];
char *description;
strcpy(name, "DB Programming");
here }
fprintf(stderr, "Error - unable to allocate required memory\n");
else {
strcpy( description, “Programming");
}
printf("Name = %s\n", name );
printf("Description: %s\n", description );
What is the
printf("String = %s, Address = %x\n", str, str);
}
here?
if( str == NULL ) {
fprintf(stderr, "Err- unable to allocate required memory\n");
} else {
strcat(str, “Database programming");
printf("String = %s, Address = %x\n", str, str);
}
free(str);
}
The output is:
String = The course is , Address = fddf32a0
String = The course is Database programming, Address = fddf36d0
What is the output here
/* suppose you want to store bigger description */
description = realloc( description, 100 * sizeof(char) );
#include <stdio.h>
#include <stdlib.h> if( description == NULL ) {
#include <string.h> fprintf(stderr, "unable to allocate required memory\n");
} else {
int main() { strcat( description, "Students of DB");
}
char name[100];
printf("Name = %s\n", name );
char *description;
printf("Description: %s\n", description );