Dynamic Memory Allocation
2
Dynamic 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 allocation
Use malloc() and calloc()
3
Dynamic Memory Allocation
 calloc()
– The size of each object is object_size byte. This function
allocates adjacent memory for the array that has n objects
– Each object is initialized with 0
– If success the call, it returns the address of allocated
memory.
– If not success the call, It returns NULL.
– Return type is void*
calloc ( n, object_size );
size of each objectnumber of objects
4
Dynamic 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 allocated
memory.
– If not success the call, It returns NULL.
– Return type is void*
malloc ( object_size );
size of each object
5
Dynamic 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
Dynamic 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
Dynamic 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
Dynamic 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
Dynamic 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” ) ;
}
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 block
10
ptr= realloc ( ptr, new_size );
11
Dynamic 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
Dynamic Memory Allocation
 free()
– You can cancel the allocated memory using the free() when
you don’t need the memory allocated by calloc() or malloc()
any more.
[Ex]
p = malloc(26);
…
free(p);
Calling free() means cancellation
the memory block pointed by p
void free(void *ptr);
13
Dynamic 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] ) ;
1000
40 bytes
1000
p
1000p
free(p)
???
14
Dynamic Memory Allocation
 garbage
– If a large number of memory block (that allocated by calloc()
or malloc() and don't need any more) is leaved without
being free, then the system is out of memory gradually,
consequentially the program can't execute normally.
– Like that, unavailable memory block called garbage.
while( 1 ) {
p = malloc( 100 ) ;
}
15
Dynamic Memory Allocation
 garbage
void main()
{
int* p ;
p = malloc(100) ;
p = malloc(100) ;
…
}
1000
100 bytes
1000
p
2000
100 bytes
2000
p
100 bytes
1000
p =malloc(100)
16
Dynamic Memory Allocation
 Receive the random number of input,
then print them in reverse order
void main() {
int k, p[20], num ; //use array
while( 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
Dynamic Memory Allocation
 Receive the random number of input,
then print them in reverse order
void main() {
int k, *p, num ; //use dynamic memory allocation
while( 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 ) ;
}
}

13. dynamic allocation

  • 1.
  • 2.
    2 Dynamic 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 allocation Use malloc() and calloc()
  • 3.
    3 Dynamic Memory Allocation calloc() – The size of each object is object_size byte. This function allocates adjacent memory for the array that has n objects – Each object is initialized with 0 – If success the call, it returns the address of allocated memory. – If not success the call, It returns NULL. – Return type is void* calloc ( n, object_size ); size of each objectnumber of objects
  • 4.
    4 Dynamic 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 allocated memory. – If not success the call, It returns NULL. – Return type is void* malloc ( object_size ); size of each object
  • 5.
    5 Dynamic 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 Dynamic 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 Dynamic 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 Dynamic 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 Dynamic 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.
    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 block 10 ptr= realloc ( ptr, new_size );
  • 11.
    11 Dynamic 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 Dynamic Memory Allocation free() – You can cancel the allocated memory using the free() when you don’t need the memory allocated by calloc() or malloc() any more. [Ex] p = malloc(26); … free(p); Calling free() means cancellation the memory block pointed by p void free(void *ptr);
  • 13.
    13 Dynamic 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] ) ; 1000 40 bytes 1000 p 1000p free(p) ???
  • 14.
    14 Dynamic Memory Allocation garbage – If a large number of memory block (that allocated by calloc() or malloc() and don't need any more) is leaved without being free, then the system is out of memory gradually, consequentially the program can't execute normally. – Like that, unavailable memory block called garbage. while( 1 ) { p = malloc( 100 ) ; }
  • 15.
    15 Dynamic Memory Allocation garbage void main() { int* p ; p = malloc(100) ; p = malloc(100) ; … } 1000 100 bytes 1000 p 2000 100 bytes 2000 p 100 bytes 1000 p =malloc(100)
  • 16.
    16 Dynamic Memory Allocation Receive the random number of input, then print them in reverse order void main() { int k, p[20], num ; //use array while( 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 Dynamic Memory Allocation Receive the random number of input, then print them in reverse order void main() { int k, *p, num ; //use dynamic memory allocation while( 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 ) ; } }