Dynamic Memory Allocation
2Dynamic Memory Allocation Need to allocate memory dynamically– If you want to change Array size as the need arise Dynam...
3Dynamic Memory Allocation calloc()– The size of each object is object_size byte. This functionallocates adjacent memory ...
4Dynamic Memory Allocation malloc()– This function allocates the memory block of object_size byte– It don’t initialize th...
5Dynamic Memory Allocation Example#include <stdlib.h>void main(void) {char *p = (char*)malloc(26) ;int k ;for( k = 0 ; k ...
6Dynamic Memory Allocation Example#include <stdlib.h>void main(void) {int *p = (int*)malloc( 26*sizeof(int) ) ;int k ;for...
7Dynamic Memory Allocation Example#include <stdlib.h>void main(void){int (*a)[4] = (int**)malloc( 3*4*sizeof(int) ) ;int ...
8Dynamic Memory Allocation Example#include <stdlib.h>typedef int IntArray[4] ;void main(void){IntArray *a = (IntArray*)ma...
9Dynamic Memory Allocation Example#include <stdlib.h>#include <string.h>typedef struct student {int std_id;char name[20];...
Dynamic Memory Allocation realloc()– Resize the previously allocated memory space– Copy the content to the newly allocate...
11Dynamic Memory Allocation Example#include <stdlib.h>void main(void) {int *p = (int*)malloc( 10*sizeof(int) ) ;int k ;fo...
12Dynamic Memory Allocation free()– You can cancel the allocated memory using the free() whenyou don’t need the memory al...
13Dynamic Memory Allocation Dangling Pointer– If you use the pointer that applied free()[Ex]char *p = malloc(40) ;free(p)...
14Dynamic Memory Allocation garbage– If a large number of memory block (that allocated by calloc()or malloc() and dont ne...
15Dynamic Memory Allocation garbagevoid main(){int* p ;p = malloc(100) ;p = malloc(100) ;…}1000100 bytes1000p2000100 byte...
16Dynamic Memory Allocation Receive the random number of input,then print them in reverse ordervoid main() {int k, p[20],...
17Dynamic Memory Allocation Receive the random number of input,then print them in reverse ordervoid main() {int k, *p, nu...
Upcoming SlideShare
Loading in …5
×

13. dynamic allocation

490 views
418 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
490
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

13. dynamic allocation

  1. 1. Dynamic Memory Allocation
  2. 2. 2Dynamic Memory Allocation Need to allocate memory dynamically– If you want to change Array size as the need arise Dynamic Memory Allocation– A process that allocate new storage while execute program– Defined calloc() and malloc() function in stdlib.h– calloc() means contiguous allocation– malloc() means memory allocationUse malloc() and calloc()
  3. 3. 3Dynamic Memory Allocation calloc()– The size of each object is object_size byte. This functionallocates adjacent memory for the array that has n objects– Each object is initialized with 0– If success the call, it returns the address of allocatedmemory.– If not success the call, It returns NULL.– Return type is void*calloc ( n, object_size );size of each objectnumber of objects
  4. 4. 4Dynamic Memory Allocation malloc()– This function allocates the memory block of object_size byte– It don’t initialize the memory space.– If success the call, it returns the address of allocatedmemory.– If not success the call, It returns NULL.– Return type is void*malloc ( object_size );size of each object
  5. 5. 5Dynamic Memory Allocation Example#include <stdlib.h>void main(void) {char *p = (char*)malloc(26) ;int k ;for( k = 0 ; k < 26 ; k++ )p[k] = ‘A’ + k ;}#include <stdlib.h>void main(void) {char *p = (char*)calloc(26, 1) ;int k ;for( k = 0 ; k < 26 ; k++ )p[k] = ‘A’ + k ;}
  6. 6. 6Dynamic Memory Allocation Example#include <stdlib.h>void main(void) {int *p = (int*)malloc( 26*sizeof(int) ) ;int k ;for( k = 0 ; k < 26 ; k++ )p[k] = k ;}#include <stdlib.h>void main(void) {int *p = (int*)calloc(26, sizeof(int) ) ;int k ;for( k = 0 ; k < 26 ; k++ )p[k] = k ;}
  7. 7. 7Dynamic Memory Allocation Example#include <stdlib.h>void main(void){int (*a)[4] = (int**)malloc( 3*4*sizeof(int) ) ;int i, j ;for( i = 0 ; i < 3 ; i++ )for( j = 0 ; j < 4 ; j++ )a[i][j] = i+j ;}
  8. 8. 8Dynamic Memory Allocation Example#include <stdlib.h>typedef int IntArray[4] ;void main(void){IntArray *a = (IntArray*)malloc( 3*sizeof(IntArray) ) ;int i, j ;for( i = 0 ; i < 3 ; i++ )for( j = 0 ; j < 4 ; j++ )a[i][j] = i+j ;}
  9. 9. 9Dynamic Memory Allocation Example#include <stdlib.h>#include <string.h>typedef struct student {int std_id;char name[20];} Student;void main(void) {Student *p = (Student*)malloc( sizeof(Student) ) ;p->std_id = 12345678 ;strcpy( p->name, “Smith” ) ;}
  10. 10. Dynamic Memory Allocation realloc()– Resize the previously allocated memory space– Copy the content to the newly allocated memory block– Free the previous memory block– Return the new memory block10ptr= realloc ( ptr, new_size );
  11. 11. 11Dynamic Memory Allocation Example#include <stdlib.h>void main(void) {int *p = (int*)malloc( 10*sizeof(int) ) ;int k ;for( k = 0 ; k < 10 ; k++ ) p[k] = k ;p = realloc( p, 20 ) ;for(; k < 20 ; k++ ) p[k] = k ;for( k = 0 ; k < 20 ; k++ )printf( “%d “, p[k] ) ;free( p ) ;}
  12. 12. 12Dynamic Memory Allocation free()– You can cancel the allocated memory using the free() whenyou don’t need the memory allocated by calloc() or malloc()any more.[Ex]p = malloc(26);…free(p);Calling free() means cancellationthe memory block pointed by pvoid free(void *ptr);
  13. 13. 13Dynamic Memory Allocation Dangling Pointer– If you use the pointer that applied free()[Ex]char *p = malloc(40) ;free(p);p[0] = ‘A’ ;printf( “%cn”, p[0] ) ;100040 bytes1000p1000pfree(p)???
  14. 14. 14Dynamic Memory Allocation garbage– If a large number of memory block (that allocated by calloc()or malloc() and dont need any more) is leaved withoutbeing free, then the system is out of memory gradually,consequentially the program cant execute normally.– Like that, unavailable memory block called garbage.while( 1 ) {p = malloc( 100 ) ;}
  15. 15. 15Dynamic Memory Allocation garbagevoid main(){int* p ;p = malloc(100) ;p = malloc(100) ;…}1000100 bytes1000p2000100 bytes2000p100 bytes1000p =malloc(100)
  16. 16. 16Dynamic Memory Allocation Receive the random number of input,then print them in reverse ordervoid main() {int k, p[20], num ; //use arraywhile( 1 ) {printf( “How many numbers?” ) ;scanf( “%d”, &num ) ;if( num < 0 ) break ;for( k = 0 ; k < num ; k++ ) scanf( “%d”, &p[k] ) ;for( k = num-1 ; k >= 0 ; k-- ) printf( “%d ”, p[k] ) ;}}
  17. 17. 17Dynamic Memory Allocation Receive the random number of input,then print them in reverse ordervoid main() {int k, *p, num ; //use dynamic memory allocationwhile( 1 ) {printf( “How many numbers?” ) ;scanf( “%d”, &num ) ;if( num < 0 ) break ;p = calloc( num, sizeof(int) ) ;for( k = 0 ; k < num ; k++ ) scanf( “%d”, &p[k] ) ;for( k = num-1 ; k >= 0 ; k-- ) printf( “%d ”, p[k] ) ;free( p ) ;}}

×