Upload
sakshi-singh
View
45
Download
0
Embed Size (px)
Citation preview
C PROGRAMMING PRESENTATION
Topic: Realloc Function
Presented By:Sakshi SinghBCA I(M)Rank- 1359
Content• Description• Declaration• Returning Value• Parameters• Example• Explanation• Output
DESCRIPTION Characteristics•Realloc function reallocates the main memory.
• It is needed when allocated memory is different from
desired memory.
• It returns the address of reallocated blocks.
• If the block cannot be reallocated it returns NULL.
Declaration:
void realloc(void *block, size_t size);
Returning Value:
This function returns a pointer to the newly allocated
memory, or NULL if the request fails.
Required Header File:#include<stdlib.h> OR #include<alloc.h>
PARAMETERS
block -- This is the pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated. If this is NULL, a new block is allocated and a pointer to it is returned by the function.
size -- This is the new size for the memory block, in bytes. If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned.
EXAMPLE:#include<stdio.h>#include<conio.h>#include<string.h>#include<alloc.h>void main(){ char *cptr; clrscr(); cptr=(char*)calloc(10,sizeof(char)); if(!cptr) { printf("Dynamic Memory Allocation Failed"); exit(1); } strcpy(cptr,"Sakshi"); printf("\nInitial Memory Allocation\n"); printf("\nString is %s\nAddress in Decimal Form is %u\nAddress in Hexadecimal Form is %x",cptr,cptr,cptr);
cptr=(char*)realloc(cptr,15); if(!cptr) { printf("Dynamic Memory Allocation Failed"); exit(1); } strcat(cptr," Singh"); printf("\n\nAfter Reallocation\n"); printf("\nString is %s\nAddress in Decimal Form is %u\nAddress in Hexadecimal Form is %x",cptr,cptr,cptr); free(cptr); getch();}
EXPLANATION•In the above program using calloc() function 10 bytes are allocated to character pointer cptr. •Firstly pointer is initialized with string “Sakshi”. •But to store more characters realloc () function is used and 20 bytes are reallocated. •Now pointer is reinitialized with “Sakshi Singh”. •And then free() is used to release the allocated memory.
*cptr
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
S k s ha i
*cptr
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
S k i S ga n hhs i
OUTPUT
BIBLIOGRAPHY
•Ashok N. Kamthane, “Programming in C”, Second Edition,2013, Pearson Education.•Yashwant Kanetkar, “Let us C” Eighth Edition, 2002.•http://www.tutorialspoint.com/c_standard_library/c_function_realloc.htm
•http://cprogrammingexpert.com/C/Tutorial/dynamic_memmory_allocation/realloc.aspx
•http://www.techonthenet.com/c_language/standard_library_functions/stdlib_h/realloc.php
THANK YOU!!!
Presented By:Sakshi SinghBCA I(M)Rank: 1359