You are on page 1of 5

(HTTPS://FRESH2REF

C TUTORIAL  C++ (HTTPS://FRESH2REFRESH.COM/CPP-TUTORIAL/)

JAVA TUTORIAL (HTTPS://FRESH2REFRESH.COM/JAVA-TUTORIAL/)

UNIX TUTORIAL (HTTPS://FRESH2REFRESH.COM/UNIX-TUTORIAL/)

SQL TUTORIAL (HTTPS://FRESH2REFRESH.COM/SQL-TUTORIAL/)

PYTHON (HTTPS://FRESH2REFRESH.COM/PYTHON-TUTORIAL/) JSP (HTTP://FRESH2REFRESH.COM/JSP-TUTORIAL/)

XML (HTTPS://FRESH2REFRESH.COM/XML-TUTORIAL/)

HEALTH EDUCATION (HTTPS://FRESH2REFRESH.COM/HEALER-BASKAR/ANATOMIC-THERAPY/) OTHERS 

C PROGRAMMING
TUTORIAL C – Dynamic memory allocation
Ad closed by
 C - LANGUAGE HISTOR PREV (HTTP://FRESH2REFRESH.COM/C/C-TIME-RELATED-
FUNCTIONS/)  
FUNCTIONS/)     NEXT (HTTP://FRESH2REFRESH.COM/C/C-TYPE- Stop seeing this ad
(HTTPS://FRESH2REFR
CASTING/)
PROGRAMMING/C-LAN Why this ad?
HISTORY/) DYNAMIC MEMORY ALLOCATION IN C:
C:

 C - LANGUAGE BASICS The process of allocating memory during program execution is called
(HTTPS://FRESH2REFR dynamic memory allocation.
PROGRAMMING/C-BAS
PROGRAM/) Ad closed by
 C - PRINTF AND SCANF Stop seeing this ad
(HTTPS://FRESH2REFR
PROGRAMMING/C-PR
Why this ad?
SCANF/)

 C - DATA TYPES
(HTTPS://FRESH2REFR
PROGRAMMING/C-DAT

 C - TOKENS AND KEYW


(HTTPS://FRESH2REFR
PROGRAMMING/C-TOK DYNAMIC MEMORY ALLOCATION FUNCTIONS IN C:
IDENTIFIERS-KEYWOR
C language o ers 4 dynamic memory allocation functions. They are,
 C - CONSTANT
(HTTPS://FRESH2REFR Ad closed by
PROGRAMMING/C-CO
Stop seeing this ad
 C - VARIABLE
(HTTPS://FRESH2REFR Why this ad?
PROGRAMMING/C-VAR

 C - OPERATORS AND
EXPRESSIONS
(HTTPS://FRESH2REFR
PROGRAMMING/C-OP
EXPRESSIONS/)

 C - DECISION CONTRO
1. malloc()
STATEMENT
2. calloc()
(HTTPS://FRESH2REFR
3. realloc()
PROGRAMMING/C-DE
4. free()
CONTROL/)

Function Syntax
 C - LOOP CONTROL ST
malloc () malloc (number *sizeof(int));
(HTTPS://FRESH2REFR
PROGRAMMING/C-LOO calloc () calloc (number, sizeof(int));
CONTROL-STATEMENT Ad closed by
realloc () realloc (pointer_name, number * sizeof(int));
 C - CASE CONTROL STA Report this ad
(HTTPS://FRESH2REFR free () free (pointer_name);
Why this ad?
PROGRAMMING/C-CAS
CONTROL-STATEMENT 1. MALLOC() FUNCTION IN C:
C:
 C - TYPE QUALIFIERS
malloc () function is used to allocate space in memory during the
(HTTPS://FRESH2REFR
execution of the program.
PROGRAMMING/C-TYP
malloc () does not initialize the memory allocated during execution.  It
QUALIFIERS/)
carries garbage value.
 C - STORAGE CLASS SP malloc () function returns null pointer if it couldn’t able to allocate
(HTTPS://FRESH2REFR requested amount of memory.
PROGRAMMING/C-STO
CLASS-SPECIFIERS/) EXAMPLE PROGRAM FOR MALLOC() FUNCTION IN C:
C:

 C - ARRAY C
1 #include <stdio.h>
(HTTPS://FRESH2REFR 2 #include <string.h>
PROGRAMMING/C-AR 3 #include <stdlib.h>
4  
 C - STRING 5 int main()
6 {
(HTTPS://FRESH2REFR 7      char *mem_allocation;
8      /* memory is allocated dynamically */
PROGRAMMING/C-STR
9      mem_allocation = malloc
malloc( 20 * sizeof
sizeof(char
char) );
10      if
if( mem_allocation== NULL )
 C - POINTER
11      {
(HTTPS://FRESH2REFR 12         printf
printf("Couldn't able to allocate requested memory\n")
13      }
PROGRAMMING/C-PO 14      else
15      {
 C - FUNCTION 16         strcpy( mem_allocation,"fresh2refresh.com");
17      }
(HTTPS://FRESH2REFR
18      printf
printf("Dynamically allocated memory content : " \
PROGRAMMING/C-FU 19             "%s\n", mem_allocation );
20      free(mem_allocation); Ad closed by
 C - ARITHMETIC FUNCT 21 }
Report this ad
(HTTPS://FRESH2REFR
COMPILE & RUN (HTTPS://COMPILERS.FRESH2REFRESH.COM/)
PROGRAMMING/C-AR Why this ad?
FUNCTIONS/)
OUTPUT:
 C - INT, CHAR VALIDAT
FUNCTIONS Dynamically allocated memory content : fresh2refresh.com
(HTTPS://FRESH2REFR
PROGRAMMING/C-INT
2. CALLOC() FUNCTION IN C:
C:
VALIDATION/)
calloc () function is also like malloc () function. But calloc () initializes
 C - BUFFER MANIPULA
the allocated memory to zero. But, malloc() doesn’t.
FUNCTIONS
(HTTPS://FRESH2REFR
EXAMPLE PROGRAM FOR CALLOC() FUNCTION IN C:
PROGRAMMING/C-BU
MANIPULATION-FUNC C
1 #include <stdio.h>
 C - TIME RELATED FUN 2 #include <string.h>
3 #include <stdlib.h>
(HTTPS://FRESH2REFR 4  
5 int main()
PROGRAMMING/C-TIM
6 {
RELATED-FUNCTIONS/ 7      char *mem_allocation;
8      /* memory is allocated dynamically */
 C - DYNAMIC MEMORY 9      mem_allocation = calloc( 20, sizeof
sizeof(char
char) );
10      if
if( mem_allocation== NULL )
ALLOCATION 11      {
(HTTPS://FRESH2REFR 12         printf
printf("Couldn't able to allocate requested memory\n")
13      }
PROGRAMMING/C-DYN 14      else
15      {
MEMORY-ALLOCATION
16          strcpy( mem_allocation,"fresh2refresh.com");
17      }
 C - TYPE CASTING FUN
18          printf
printf("Dynamically allocated memory content   : " \
(HTTPS://FRESH2REFR 19                 "%s\n", mem_allocation );
20          free(mem_allocation);
PROGRAMMING/C-TYP 21 }
CASTING/)
COMPILE & RUN (HTTPS://COMPILERS.FRESH2REFRESH.COM/)
 C - MISCELLANEOUS F OUTPUT:
(HTTPS://FRESH2REFR
Dynamically allocated memory content : fresh2refresh.com
PROGRAMMING/C-
MISCELLANEOUS-FUN

 C - STRUCTURE 3. REALLOC() FUNCTION IN C:


(HTTPS://FRESH2REFR
realloc () function modi es the allocated memory size by malloc () and
PROGRAMMING/C-STR
calloc () functions to new size.
 C - TYPEDEF If enough space doesn’t exist in memory of current block to extend,
(HTTPS://FRESH2REFR new block is allocated for the full size of reallocation, then copies the
PROGRAMMING/C-TYP existing data to new block and then frees the old block.

 C - UNION
 4. FREE() FUNCTION IN C:
C:
(HTTPS://FRESH2REFR
PROGRAMMING/C-UN free () function frees the allocated memory by malloc (), calloc (),
 C - PREPROCESSOR D realloc () functions and returns the memory to the system.
(HTTPS://FRESH2REFR
PROGRAMMING/C- EXAMPLE PROGRAM FOR REALLOC() AND FREE()
PREPROCESSOR-DIRE
FUNCTIONS IN C:
C:
C
 C - FILE HANDLING
1 #include <stdio.h>
(HTTPS://FRESH2REFR 2 #include <string.h>
3 #include <stdlib.h>
PROGRAMMING/C-FIL
4  
HANDLING/) 5 int main()
6 {
 C PROGRAMS 7     char
char *mem_allocation;
8     /* memory is allocated dynamically */
(HTTPS://FRESH2REFR 9     mem_allocation = malloc
malloc( 20 * sizeof
sizeof(char
char) );
10     if
if( mem_allocation == NULL )
PROGRAMMING/C-PRO
11     {
12         printf
printf("Couldn't able to allocate requested memory\n")
 C INTERVIEW QUESTIO
13     }
ANSWERS 14     else
else
15     {
(HTTPS://FRESH2REFR 16        strcpy( mem_allocation,"fresh2refresh.com");
PROGRAMMING/C-INT 17     }
18     printf
printf("Dynamically allocated memory content  : " \
QUESTIONS-ANSWERS 19            "%s\n", mem_allocation );
20     mem_allocation=realloc(mem_allocation,100*sizeof
sizeof(char
char));
 DISCUSSION FORUMS 21     if
if( mem_allocation == NULL )
22     {
(HTTPS://FRESH2REFR
23         printf
printf("Couldn't able to allocate requested memory\n")
FORUMS/) 24     }
25     else
else
26     {
27         strcpy( mem_allocation,"space is extended upto " \
C INTERVIEW 28                                "100 characters");
QUESTIONS 29     }
30     printf
printf("Resized memory : %s\n", mem_allocation );
31     free(mem_allocation);
32 }
 C INTERVIEW QUESTIO
ANSWERS
COMPILE & RUN (HTTPS://COMPILERS.FRESH2REFRESH.COM/)
(HTTPS://FRESH2REFR
PROGRAMMING/C-INT OUTPUT:
QUESTIONS-ANSWERS
Dynamically allocated memory content : fresh2refresh.com
 C PROGRAMS
Resized memory : space is extended upto 100 characters
(HTTPS://FRESH2REFR
PROGRAMMING/C-PRO

 C PROGRAMMING & TU
DIFFERENCE BETWEEN  STATIC MEMORY ALLOCATION
AND DYNAMIC MEMORY ALLOCATION IN C:
C:
(HTTPS://FRESH2REFR
PROGRAMMING/)
Static memory allocation Dynamic memory
 DISCUSSION FORUMS allocation
(HTTPS://FRESH2REFR
In static memory allocation, memory In dynamic memory
FORUMS/)
is allocated while writing the C allocation, memory is
program. Actually, user requested allocated while executing
memory will be allocated at compile the program. That means
time. at run time.

Memory size can’t be modi ed while Memory size can be


execution.  modi ed while execution. 
Example: array Example: Linked list
Ad closed by DIFFERENCE BETWEEN MALLOC() AND CALLOC()
FUNCTIONS IN C:
C:
Stop seeing this
malloc() calloc()
Why this ad?
It allocates only single block It allocates multiple blocks of
of requested memory requested memory

int *ptr;ptr = malloc( 20 * int *ptr;Ptr = calloc( 20, 20 *


sizeof(int) );For the above, sizeof(int) );For the above, 20 blocks
20*4 bytes of memory only of memory will be created and each
allocated in one block. contains 20*4 bytes of memory.
Total = 80 bytes Total = 1600 bytes

malloc () doesn’t initializes calloc () initializes the allocated


the allocated memory. It memory to zero
contains garbage values

type cast must be done Same as malloc () function int


since this function returns *ptr;ptr = (int*)calloc( 20, 20 *
void pointer int *ptr;ptr = sizeof(int) );
(int*)malloc(sizeof(int)*20
);

PREV (HTTP://FRESH2REFRESH.COM/C/C-TIME-RELATED-
FUNCTIONS/)      NEXT
FUNCTIONS/)  NEXT (HTTP://FRESH2REFRESH.COM/C/C-TYPE-
CASTING/)

Ad closed by
Stop seeing this ad

Why this ad?

Improve Your Resume Memory allocation in C


Ad Grammarly fresh2refresh.com

3일만 공짜로 해보자, 작심코딩 C Interview Questions and Answers


Ad codeit - 코드잇 fresh2refresh.com

여대생 세정이 무료보기 C Arrays with examples


Ad TOPTOON fresh2refresh.com

C - stdlib.h library functions C - Structure using Pointer


fresh2refresh.com fresh2refresh.com

C - conio.h library functions C Function with examples


fresh2refresh.com fresh2refresh.com

Share this website !!!

(http://www.facebook.com/sharer.php?u=https%3A%2F%2Ffresh2refresh.com%2Fc-programming%2Fc-d

(http://twitter.com/share?url=https%3A%2F%2Ffresh2refresh.com%2Fc-programming%2Fc-dynamic-memory-allocation%

(http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Ffresh2refresh.com%2Fc-programming%2Fc-dynam
Copyright 2019 ©  fresh2refresh.com (https://fresh2refresh.com/).  All rights reserved

(https://play.google.com/store/apps/details?id=com.zeeroapps.freshrefresh)
About us (http://fresh2refresh.com/about-us/) | Contact us (http://fresh2refresh.com/about-us/contact-us/) | Privacy policy
(http://fresh2refresh.com/about-us/privacy-policy/)
 

You might also like