100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.6 TrustPilot
logo-home
Class notes

Computer system

Rating
-
Sold
-
Pages
30
Uploaded on
24-07-2023
Written in
2017/2018

Computer system, computer science, computer skills, computer language

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Study Level
Examinator
Subject
Unit

Document information

Uploaded on
July 24, 2023
Number of pages
30
Written in
2017/2018
Type
Class notes
Professor(s)
Prof
Contains
All classes

Subjects

Content preview

Diffrence between malloc() and calloc()

calloc() malloc()

calloc() initializes the allocated memory with malloc() initializes the allocated memory with garbage
0 value. values.

Number of arguments is 2 Number of argument is 1


Syntax : Syntax :
(cast_type *)calloc(blocks , size_of_block); (cast_type *)malloc(Size_in_bytes);




realloc(): changes memory size that is already allocated to a variable.
Or
If the previously allocated memory is insufficient or more than required, you can change the
previously allocated memory size using realloc().
 If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by
realloc() function. In short, it changes the memory size. By using realloc() we can create
the memory dynamically at middle stage. Generally by using realloc() we can
reallocation the memory. Realloc() required 2 arguments of type void*, size_type. Void*
will indicates previous block base address, size-type is data type size. Realloc() will
creates the memory in bytes format and initial value is garbage.


syntax
ptr=realloc(ptr, new-size)
Example
int *x;
x=(int*)malloc(50 * sizeof(int));
x=(int*)realloc(x,100); //allocated a new memory to variable x




C PROGRAMMING Page 231

,Example
void*realloc(void*, size-type);
int *arr;
arr=(int*)calloc(5, sizeof(int));
.....
........
....
arr=(int*)realloc(arr,sizeof(int)*10);
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size of array: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Address of previously allocated memory: ");
for(i = 0; i < n1; ++i)
printf("%u\t",ptr + i);
printf("\nEnter new size of array: ");
scanf("%d", &n2);
ptr = realloc(ptr, n2);
for(i = 0; i < n2; ++i)
printf("%u\t", ptr + i);
return 0;
}




C PROGRAMMING Page 232

, free()
When your program comes out, operating system automatically release all the memory allocated
by your program but as a good practice when you are not in need of memory anymore then you
should release that memory by calling the function free().
The memory occupied by malloc() or calloc() functions must be released by calling free()
function. Otherwise, it will consume memory until program exit.
Or
Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its
own. You must explicitly use free() to release the space.
Syntax:
free(ptr);


Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;


printf("Enter number of elements: ");
scanf("%d", &num);


ptr = (int*) malloc(num * sizeof(int)); //memory allocated using malloc
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);

C PROGRAMMING Page 233
$23.17
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
akhilagouri

Get to know the seller

Seller avatar
akhilagouri A Level Notes
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
2 year
Number of followers
0
Documents
107
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions