1 / 13

Dynamic Memory Allocation

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

kadeem
Télécharger la présentation

Dynamic Memory Allocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Memory Allocation 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() • calloc() • realloc() • free()

  3. 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. • 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

  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. • It is declared with two arguments. • The format is as follows: ptr=(int*)calloc(4,2); • 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. • 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. • 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.

  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. 

More Related