Movatterモバイル変換


[0]ホーム

URL:


C Dynamic Memory Allocation

C Programming Tutorials

Overview of C LanguageC Language FundamentalsData Input and OutputDecision Control StatementsLoop Control StatementsFunctionsPreprocessors and Header FilesArrays and StringsPointersStructure and UnionFile Handling

The functions used to manipulate memory in C programming aremalloc(),calloc(), andrealloc(). These commonly used functions are available through thestdlib() library, so you must include this library in your program to use them.

Table of Contents

C - Dynamic Memory Allocation Functions

FunctionSyntax
malloc()malloc (number *sizeof(int));
calloc()calloc (number, sizeof(int));
realloc()realloc (pointer_name, number * sizeof(int));
free()free (pointer_name);

malloc function

  • malloc function allocates space in memory during the program's execution.
  • malloc function does not initialize the space in memory allocated during execution. It carries garbage value.
  • malloc function returns anull pointer if it couldn't allocate the requested amount of memory.

Example Program formalloc() in C

Example:

#include<stdio.h>#include<string.h>#include<stdlib.h>void main(){    char *mem_alloc;    //memory allocated dynamically    mem_alloc = malloc( 15 * sizeof(char) );    if(mem_alloc== NULL )    {        printf("Couldn't able to allocate requested memory\n");    }    else    {        strcpy( mem_alloc,"w3schools.in");    }    printf("Dynamically allocated memory content : %s\n", mem_alloc );    free(mem_alloc);}

Program Output:

Dynamically allocated memory content : w3schools.in

calloc function

  • Thecalloc() andmalloc() function is similar. Butcalloc() allocates memory for zero-initializes. However,malloc() does not.

Example Program forcalloc() in C

Example:

#include<stdio.h>#include<string.h>#include<stdlib.h>void main(){    char *mem_alloc;    //memory allocated dynamically    mem_alloc = calloc( 15, sizeof(char) );    if( mem_alloc== NULL )    {        printf("Couldn't able to allocate requested memory\n");    }    else    {        strcpy( mem_alloc,"w3schools.in");    }    printf("Dynamically allocated memory content : %s\n", mem_alloc );    free(mem_alloc);}

Program Output:

Dynamically allocated memory content : w3schools.in

realloc function

  • Therealloc() function modifies the allocated memory size to a new size by themalloc() andcalloc() functions.
  • If enough space doesn't exist in the current block's memory to expand, a new block is allocated for the total size of the reallocation, then copies the existing data to the new block and frees the old block.

free function

Thefree() function releases the memory allocated by themalloc(),calloc(),realloc() functions.

Example Program forrealloc() andfree()

Example:

#include<stdio.h>#include<string.h>#include<stdlib.h>void main(){    char *mem_alloc;    //memory allocated dynamically    mem_alloc = malloc( 20 * sizeof(char) );    if( mem_alloc == NULL )    {        printf("Couldn't able to allocate requested memory\n");    }    else    {        strcpy( mem_alloc,"w3schools.in");    }    printf("Dynamically allocated memory content  : " \ "%s\n", mem_alloc );    mem_alloc=realloc(mem_alloc,100*sizeof(char));    if( mem_alloc == NULL )    {        printf("Couldn't able to allocate requested memory\n");    }    else    {        strcpy( mem_alloc,"space is extended upto 100 characters");    }    printf("Resized memory : %s\n", mem_alloc );    free(mem_alloc);}

Program Output:

Dynamically allocated memory content : w3schools.in

Resized memory: space is extended up to 100 characters



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram

Related Content

C Memory Management

[8]ページ先頭

©2009-2025 Movatter.jp