Developer community
0 votes
59 views
asked by Expert (13.2k points) 29 44 63 | 59 views

1 Answer

0 votes
Allocation of memory at the time of execution is called dynamic memory allocation. It is done using the standard library functions malloc() and calloc(). It is defined in "stdlib.h". malloc(): used to allocate required number of bytes in memory at runtime. It takes one argument, viz. size in bytes to be allocated.

Syntax:

void * malloc(size_t size);

Example:

a = (int*) malloc(4);

4 is the size (in bytes) of memory to be allocated.

calloc(): used to allocate required number of bytes in memory at runtime. It needs two arguments viz.,

1. total number of data and

2. size of each data.

Syntax:

void * calloc(size_t nmemb, size_t size);

Example:

a = (int*) calloc(8, sizeof(int));

Here sizeof indicates the size of the data type and 8 indicates that we want to reserve space for

storing 8 integers.

 Differences between malloc() and calloc() are:

1. Number of arguments differ.

2. By default, memory allocated by malloc() contains garbage values. Whereas memory allocated by calloc() contains all zeros.
answered by Guru (39.1k points) 11 24 38

Related questions

0 votes
3 answers
asked by john Guru (27.1k points) 35 53 114 | 75 views
0 votes
1 answer
asked by john Guru (27.1k points) 35 53 114 | 25 views
0 votes
1 answer
asked by john Guru (27.1k points) 35 53 114 | 38 views
+2 votes
1 answer
0 votes
2 answers
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,754 questions
4,046 answers
117 comments
10,709 users