Machine-translated by Google. Questions are authored in English.
Top

Memory Allocation Questions and Answers

31.

The function ____ obtains block of memory dynamically.

Answer: C

Correct answer: Both a & b

malloc and calloc both obtain blocks of memory dynamically from the heap. They differ in how the size is expressed and in whether the block is zeroed, not in where the storage comes from.

malloc takes a single total and leaves the contents arbitrary; calloc takes a count and an element size and clears the block. free is the counterpart that returns storage rather than obtaining it, and realloc resizes a block already held.

Please log in or register to report this question.

Answer: D

Correct answer: calloc() initialises the allocated memory to zero while malloc() does not

calloc() takes the number of elements and the size of each, and clears the whole block to zero before returning it. malloc() takes a single size in bytes and leaves the contents indeterminate, so reading them before assignment is undefined behaviour.

Please log in or register to report this question.

33.

How will you free the memory allocated by the following program?

#include <stdio.h>
#include <stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}

 

Answer: D

Correct answer: free(p);

The pointer that malloc returned is the one free must be given, so free(p) releases the block of row pointers.

Note what has actually been allocated: only the array of MAXROW pointers. Had the rows themselves been allocated in a loop, each would need freeing individually before free(p) - releasing the outer array first would destroy the only record of the row addresses and leak every one of them.

Please log in or register to report this question.

34.

Output?

#include <stdio.h>
#include <stdlib.h>
  
void fun(int *a)
{
    a = (int*)malloc(sizeof(int));
}
  
int main()
{
    int *p;
    fun(p);
    *p = 6;
    printf("%d\n",*p);
    return(0);
}

Answer: A

Correct answer: May not work

The pointer is passed by value, so fun receives a copy. Assigning the result of malloc to that copy changes nothing in main, where p is still uninitialised.

Writing *p = 6 then dereferences an indeterminate pointer, which is undefined behaviour - it may crash, may appear to work, may corrupt something else. To change the caller's pointer the function would need a int ** parameter, or it would have to return the new address.

Please log in or register to report this question.

35.

Can I increase the size of statically allocated array?

Answer: B

Correct answer: No

A statically allocated array has its size fixed when the program is compiled, and there is no mechanism to change it at run time.

Obtaining a larger one means allocating fresh storage dynamically and copying the contents across - which is exactly what realloc does for heap blocks. This inflexibility is the principal reason dynamic allocation exists, and it is why every growable structure in C is built on the heap.

Please log in or register to report this question.

Answer: A

Correct answer: points to memory that has already been freed

Calling free() releases the block but leaves the pointer holding its old address, and using it afterwards is undefined behaviour. Setting the pointer to NULL immediately after freeing it is the standard guard against this.

Please log in or register to report this question.

37.

What will be the output of the program?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    union test
    {
        int i;
        float f;
        char c;
    };
    union test *t;
    t = (union test *)malloc(sizeof(union test));
    t->f = 10.10f;
    printf("%f", t->f);
    return 0;
}

 

Answer: C

Correct answer: 10.100000

The value is written to the union's float member and read back through the same member, so nothing is reinterpreted and the value is intact.

It prints as 10.100000 because %f defaults to six decimal places. Remember that a union holds only one member at a time, all sharing the same storage: writing t->f destroys whatever t->i or t->c contained, and reading a different member afterwards would reinterpret the float's bits.

Please log in or register to report this question.

38.

Assume integer is 2 bytes wide. How many bytes will be allocated for the following code?

#include <stdio.h>
#include <stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int (*p)[MAXCOL];
    p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
    return 0;
}

 

Answer: C

Correct answer: 24 bytes

p is a pointer to an array of MAXCOL integers, so *p is that array and

\[ \mathtt{sizeof(*p)} = 4 \times 2 = 8\ \mathrm{bytes} \]

\[ \mathtt{MAXROW} \times 8 = 3 \times 8 = 24\ \mathrm{bytes} \]

The declaration is worth reading carefully: int (*p)[MAXCOL] is a pointer to an array, whereas int *p[MAXCOL] without the parentheses would be an array of pointers - a different type allocating a different amount.

Please log in or register to report this question.

39.

In which memory segment are string literals typically stored in a C program ?

Answer: C

Correct answer: The read-only data segment

String literals are placed in a read-only area loaded with the program image. Attempting to modify one through a char pointer compiles but produces undefined behaviour, usually a segmentation fault.

Please log in or register to report this question.

40.

Memory allocation using malloc() is done in?

Answer: C

Correct answer: Heap area

malloc takes storage from the heap, a region the program grows at run time. That is what gives the block a lifetime independent of any function: it persists until free is called, whatever returns in the meantime.

The static area holds globals and static variables, laid out before the program starts, and the stack holds automatic variables, reclaimed on return. It is the heap's independence from function lifetimes that makes it the only place a data structure can be built and returned.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Memory Allocation Questions and Answers || Memory Allocation MCQ Questions and Answers || Memory Allocation GK Questions and Answers || Memory Allocation GK MCQ Questions || Memory Allocation Multiple Choice Questions and Answers || Memory Allocation GK || GK on Memory Allocation || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming