0% found this document useful (0 votes)
4 views

Lec12_Memory Management (1) (2)

The document discusses memory management in programming, focusing on static and dynamic memory allocation. It explains the differences between static allocation (managed by the compiler) and dynamic allocation (managed by the programmer using functions like malloc, calloc, and realloc). It also highlights the importance of deallocating memory to prevent memory leaks and provides examples of memory allocation and usage in C programming.

Uploaded by

abood5002016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lec12_Memory Management (1) (2)

The document discusses memory management in programming, focusing on static and dynamic memory allocation. It explains the differences between static allocation (managed by the compiler) and dynamic allocation (managed by the programmer using functions like malloc, calloc, and realloc). It also highlights the importance of deallocating memory to prevent memory leaks and provides examples of memory allocation and usage in C programming.

Uploaded by

abood5002016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Database

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.

) • returns the start address of the new block of reserved


memory.
• do not lose this!

Functio • Important points:


• once allocated, memory is reserved until:
• it is freed explicitly, using :

n
• free() function.
• program termination.

• If you lose a pointer to dynamically allocated memory, it


stays reserved anyway.
double *a;
Static
double b;
a = &b;
*a = 3.14;
printf("%lf\n", *a);

The output is:


3.14

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

We need to free the memory


Memory Leaks

What’s a memory leak? What’s so bad about this?


When you lose pointer to dynamically allocated You lose access to data in memory.
memory. Amount of memory available is not infinite.
When you forget to deallocate dynamically You will lose certain amount of memory till the
allocated memory. end of the program’s lifetime.
Deallocating
Dynamically Allocated
Memory

• You must deallocate (release) all memory that you allocate.

• 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

Calloc() • What is calloc()?


• The calloc() function is also known as contiguous allocation function
• Allocates a specific amount of memory and initializes it to zero
• The function can be cast to the desired type when it returns to a void
pointer to the memory location
Malloc() VS Calloc()
• Re-allocation method in C is used to dynamically change the memory
allocation of a previously allocated memory
• Attempts to resize the memory block pointed to by ptr.
• Function signature:
• void *realloc(void *ptr, size_t size)

realloc() • ptr: Pointer to memory block to be reallocated


• Size: the new size of the memory block in bytes

Function • Return Value


This function returns a pointer to the newly allocated memory, or NULL if
the request fails.

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");

What is /* allocate memory dynamically */

the output description = (char *) malloc( 200 * sizeof(char) );


if( description == NULL ) {

here }
fprintf(stderr, "Error - unable to allocate required memory\n");

else {
strcpy( description, “Programming");
}
printf("Name = %s\n", name );
printf("Description: %s\n", description );

free(description); The output is:


Name = DB Programming
} Description: Programming
#include <stdio.h>
#include <stdlib.h>
void main() {
char *str;
str = (char *) calloc(15 , sizeof(char));
if( str == NULL ) {
fprintf(stderr, "Err- unable to allocate required memory\n");
} else {
strcpy(str, "The course is ");

What is the
printf("String = %s, Address = %x\n", str, str);
}

output /* Reallocating memory */


str = (char *) realloc(str, 25);

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 );

strcpy(name, "Zara Ali");


/* release memory using free() function */
free(description);
/* allocate memory dynamically */ }
description = (char *) malloc( 30 * sizeof(char) );

if( description == NULL ) {


fprintf(stderr,"unable to allocate required memory\n");
The output is:
} else {
strcpy( description, "DB student.");
Name = Zara Ali
} Description: DB student.Students of DB
Acknowledgment

Slides were prepared by Eng. Lina Hammad

You might also like