Professional Documents
Culture Documents
XML (HTTPS://FRESH2REFRESH.COM/XML-TUTORIAL/)
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 - 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 - 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.
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
(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/)