Memory allocation in c

muhammedhishammelayi 945 views 15 slides Aug 05, 2014
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

enjoy reading..


Slide Content

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Dynamic memory allocation in C MUHAMMED THANVEER.M [email protected] www.facebook.com/ thanveer twitter.com/username in.linkedin.com/in/ profilename 9526960445

The process of allocating memory during program execution is called dynamic memory allocation. 4 dynamic memory allocation functions S.No Function Syntax malloc () malloc (number * sizeof ( int )); calloc () calloc (number, sizeof ( int )); realloc () realloc ( pointer_name , number * sizeof ( int )); free () free ( pointer_name );

1. malloc () function in C: malloc () function is used to allocate space in memory during the execution f the program. malloc () does not initialize the memory allocated during execution.  It carries garbage value. malloc () function returns null pointer if it couldn’t able to allocate requested amount of memory.

EXAMPLE # include < stdio.h > #include < string.h > #include < stdlib.h >   int main() {      char * mem_allocation ;      /* memory is allocated dynamically */      mem_allocation = malloc ( 20 * sizeof ( char ) );      if ( mem_allocation == NULL )      {          printf ("Couldn't able to allocate requested memory\n");      }      else      {          strcpy ( mem_allocation ,“[email protected] "); output: [email protected]      }      printf ("Dynamically allocated memory content : " \             "%s\n", mem_allocation );      free( mem_allocation ); }

2. calloc () function in C : calloc () function is also like malloc () function. But calloc () initializes the allocated memory to zero. But, malloc () doesn’t.

EXAMPLE # include < stdio.h > #include < string.h > #include < stdlib.h >   int main() {      char * mem_allocation ;      /* memory is allocated dynamically */      mem_allocation = calloc ( 20, sizeof ( char ) ); Output: [email protected]      if ( mem_allocation == NULL )      {          printf ("Couldn't able to allocate requested memory\n");      }      else      {          strcpy ( mem_allocation ,“[email protected] ");      }          printf ("Dynamically allocated memory content   : " \                 "%s\n", mem_allocation );          free( mem_allocation ); }

3. realloc () function in C: realloc () function modifies the allocated memory size by malloc () and calloc () functions to new size. If enough space doesn’t exist in memory of current block to extend, new block is allocated for the full size of reallocation, then copies the existing data to new block and then frees the old block. 4. free() function in C: free () function frees the allocated memory by malloc (), calloc (), realloc () functions and returns the memory to the system.

EXAMPLE // insert extra numbers #include< stdio.h >//header #include< stdlib.h > int main()//main function { int int_n,int_i,int_j,int_m ,* ptr_b ;//local variable declaration char str_a ; printf ("enter number of element");//by user scanf ("% d",&int_n );//read and assign ptr_b =( int *) calloc ( int_n,sizeof ( int ));// malloc memory allocation if( ptr_b ==NULL) { printf (" ERROR!memory not allocated"); exit(0); } printf (" Now,enter the element");//by user for( int_i =0;int_i< int_n ;++ int_i )//check condition { scanf ("% d",ptr_b+int_i );//block of statement }

printf ("do you want to enter more numbers?.y/n"); scanf ("% s",&str_a ); if( str_a =='y'|| str_a =='Y')//check condition using if { printf ("enter the number of extra element:"); scanf ("% d",&int_m ); printf ("enter the new elements:"); ptr_b =( int *) realloc ( ptr_b,int_n+int_m );//create realloc memory for( int_j = int_n;int_j <( int_n+int_m ); int_j ++)//check condition using loop { scanf ("% d",ptr_b+int_j ); } for( int_i =0;int_i<( int_n+int_m );++ int_i ) { printf ("%d",*( ptr_b+int_i ));//print the value } } getch (); free( ptr_b );//clear the allocated memory return 0; }

OUT PUT Enter number of element 3 Now,enter the element 4 5 6 Do you want to enter more numbers?.y/n Y Enter the number of extra elements 2 Enter the new element 8 9 45689

THANK YOU

If this presentation helped you, please visit our page  facebook.com/ baabtra  and like it. Thanks in advance .    www.baabtra.com  |  www.massbaab.com  | www.baabte.com

Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam , Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam , Kerala, India. Email: [email protected]
Tags