edited by
3,237 views
3 votes
3 votes

Match the following $:$

$\begin{array}{}  \text{a.} & \text{calloc()} & \text{i.} & \text{Frees previously allocated space} \\ \text{b.} & \text{free()} & \text{ii.} & \text{Modifies previously allocated space} \\ \text{c.} & \text{malloc()} & \text{iii.} & \text{Allocates space for array} \\ \text{d.} & \text{realloc()} & \text{iv.} & \text{Allocates requested size of space} \\  \end{array}$

$\textbf{Codes :}$

  1. $\text{a-iii, b-i, c-iv, d-ii}$
  2. $\text{a-iii, b-ii, c-i, d-iv}$
  3. $\text{a-iii, b-iv, c-i, d-ii}$
  4. $\text{a-iv, b-ii, c-iii, d-i}$
edited by

2 Answers

4 votes
4 votes

Answer : A

a. calloc( )                         iii. Allocates space for array
b. free( )                            i. Frees previously modified
c. malloc( )                        iv. Allocates requested size of space
d. realloc( )                        ii. Modifies previously allocated space 

malloc()
The name malloc stands for "memory allocation". The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form.
Syntax of malloc()
ptr=(cast-type*)malloc(byte-size)
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.
ptr=(int*)malloc(100*sizeof(int));

calloc()
The name calloc stands for "contiguous allocation". The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.
Syntax of calloc()
ptr=(cast-type*)calloc(n,element-size);
This statement will allocate contiguous space in memory for an array of nelements. For example:
ptr=(float*)calloc(25,sizeof(float));
This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes.

free()
Dynamically allocated memory with either calloc() or malloc() does not get return on its own. The programmer must use free() explicitly to release space.
syntax of free()
free(ptr);
This statement cause the space in memory pointer by ptr to be deallocated.

realloc()
If the previously allocated memory is insufficient or more than sufficient. Then, you can change memory size previously allocated using realloc().
Syntax of realloc()
ptr=realloc(ptr,newsize);
Here, ptr is reallocated with size of newsize.

2 votes
2 votes

Answer A

a) calloc( ) : allocates contiguous memory -> Allocates space for array -> iii

b)  free( ): deallocate memory -> Frees previously modified -> i

c)malloc():  Allocates requested size of space->iv

d) realloc() ->Modifies previously allocated space ->ii

Answer:

Related questions

1 votes
1 votes
1 answer
2
1 votes
1 votes
2 answers
3
go_editor asked Jul 18, 2016
1,255 views
Trace the error:void main() { int *b, &a; *b=20; printf(“%d, %d”, a, *b) }No errorLogical errorSyntax errorSemantic error
3 votes
3 votes
1 answer
4
go_editor asked Jul 17, 2016
1,710 views
Arrays in C language can have ____ with reference to memory representation.n-subscriptstwo-subscriptsonly one subscriptthree subscripts only