Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Allocating memory dynamically in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 73
    Comment on it

    In C programming, you will allocate memory dynamically by using the functions defined in the header files.

     

    These functions are found in <stdlib.h> header file.

     

    S.N. Function & Description
    1

    void *calloc(int num, int size);

    This function allocates an array of num elements each of which size in bytes will be size.

    2

    void free(void *address);

    This function releases a block of memory block specified by address.

    3

    void *malloc(int num);

    This function allocates an array of num bytes and leave them initialized.

    4

    void *realloc(void *address, int newsize);

    This function re-allocates memory extending it upto newsize.

     

     

    While programming, if you know the size of an array, then it is easy and you can define it as an array. 

     

    char name[100];

     

    In those situation where you have no idea about the length of the text you need to store then these functions will be handy.

     

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
    
       char name[100];
       char *description;
    
       strcpy(name, "Zara Ali");
    
       /* allocate memory dynamically */
       description = malloc( 200 * sizeof(char) );
    	
       if( description == NULL ) {
          fprintf(stderr, "Error - unable to allocate required memory\n");
       }
       else {
          strcpy( description, "Zara ali a DPS student in class 10th");
       }
       
       printf("Name = %s\n", name );
       printf("Description: %s\n", description );
    }

     

    Same program with the different function can be used

     

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
    
       char name[100];
       char *description;
    
       strcpy(name, "Zara Ali");
    
       /* allocate memory dynamically */
       description = malloc( 30 * sizeof(char) );
    	
       if( description == NULL ) {
          fprintf(stderr, "Error - unable to allocate required memory\n");
       }
       else {
          strcpy( description, "Zara ali a DPS student.");
       }
    	
       /* suppose you want to store bigger description */
       description = realloc( description, 100 * sizeof(char) );
    	
       if( description == NULL ) {
          fprintf(stderr, "Error - unable to allocate required memory\n");
       }
       else {
          strcat( description, "She is in class 10th");
       }
       
       printf("Name = %s\n", name );
       printf("Description: %s\n", description );
    
       /* release memory using free() function */
       free(description);
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: