Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase.

Similar presentations


Presentation on theme: "Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase."— Presentation transcript:

1 Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase or decrease as your program runs. Up until this point, the memory allocation for your program has been handled automatically when compiling. However, sometimes the computer doesn't know how much memory to set aside (for example, when you have an unsized array). The dynamic memory allocation functions give you the power to dynamically allocate memory for your variables at RUN- TIME (while the program is running). Before for the programs, memory was allocated when the program was compiled (i.e.COMPILE-TIME).

2 MEMORY ALLOCATION FUNCTIONS malloc() malloc() calloc() calloc() realloc() realloc() free() free()

3 This function is used to allocate memory space in bytes to the variables of different types. This function is used to allocate memory space in bytes to the variables of different types. malloc requires one argument - the number of bytes you want to allocate dynamically. malloc requires one argument - the number of bytes you want to allocate dynamically. If the memory allocation was successful, malloc will return base address of allocated memory to this to a pointer variable. If the memory allocation was successful, malloc will return base address of allocated memory to this to a pointer variable. The prototypes are declared in alloc.h and stdlib.h The prototypes are declared in alloc.h and stdlib.h

4 int *ptr; ptr=(datatype*) malloc(size); ptr=(int*) malloc(20);

5 #include<alloc.h> Void main() { int k,*p,j=0; printf(“how many numbers?”); scanf(“%d”,&k);p=(int*)malloc(k*sizeof(int));while(j!=k){printf(“number=%d”,j+1);scanf(“%d”,p+j);j++;}

6 j=0; printf(“the numbers are:”); while(j!=k)printf(“%d”,*(p+j));j++;}

7 calloc() This function is used for allocating multiple blocks of memory. This function is used for allocating multiple blocks of memory. It is declared with two arguments. It is declared with two arguments. The format is as follows: The format is as follows:ptr=(int*)calloc(4,2); It allocates 4 blocks of memory and each block contains 2 bytes. It allocates 4 blocks of memory and each block contains 2 bytes.

8 #include<alloc.h> Void main() { int k,*p,j=0; printf(“how many numbers?”); scanf(“%d”,&k);p=(int*)calloc(k*sizeof(int),2);while(j!=k){printf(“number=%d”,j+1);scanf(“%d”,p+j);j++;}

9 j=0; printf(“the numbers are:”); while(j!=k)printf(“%d”,*(p+j));j++;}

10 realloc() This function reallocates main memory. This function reallocates main memory. It is used to shrink or enlarge the previously allocated memory with malloc() and calloc(). It is used to shrink or enlarge the previously allocated memory with malloc() and calloc().Ptr=(int*)malloc(20);Ptr=(int*)realloc(ptr,10);

11 free() This function is used to release the memory allocated by the memory allocating functions. This function is used to release the memory allocated by the memory allocating functions. Using this function the wastage of memory is prevented. Using this function the wastage of memory is prevented.free(ptr);

12 Memory Leak A condition caused by a program that does not free up the extra memory it allocates. In programming languages, such as C/C++, the programmer can dynamically allocate additional memory to hold data and variables that are required for the moment, but not used throughout the program. When those memory areas are no longer needed, the programmer must remember to deallocate them. A condition caused by a program that does not free up the extra memory it allocates. In programming languages, such as C/C++, the programmer can dynamically allocate additional memory to hold data and variables that are required for the moment, but not used throughout the program. When those memory areas are no longer needed, the programmer must remember to deallocate them.

13 Memory Leak(Contd..) When memory is allocated, but not deallocated, a memory leak occurs (the memory has leaked out of the computer). If too many memory leaks occur, they can usurp all of memory and bring everything to a halt or slow the processing considerably. When memory is allocated, but not deallocated, a memory leak occurs (the memory has leaked out of the computer). If too many memory leaks occur, they can usurp all of memory and bring everything to a halt or slow the processing considerably.


Download ppt "Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase."

Similar presentations


Ads by Google