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

Memory Allocation Questions and Answers

Answer: B

Correct answer: "ptr = calloc(m, n)" is equivalent to following ptr = malloc(m * n); memset(ptr, 0, m * n);

calloc(m, n) does two things: it allocates \( m \times n \) bytes, and it sets every one of them to zero. Reproducing it with malloc therefore needs both steps - the allocation and the memset.

The option offering malloc(m * n) alone gets the size right but leaves the block holding arbitrary data. The two options using m or n alone allocate far too little, since calloc's two arguments are multiplied. That multiplication is also where calloc checks for overflow, which a hand-written product does not.

Please log in or register to report this question.

Answer: B

Correct answer: allocates the specified number of bytes and initializes them to zero

calloc does two things where malloc does one: it allocates the storage and sets every byte of it to zero.

It also takes its size as a count and an element size rather than a single total, and it is required to detect overflow in the product of the two - a check a hand-written malloc(m * n) does not get. Resizing an existing block is realloc's work, described by the third option.

Please log in or register to report this question.

13.

If malloc() successfully allocates memory it returns the number of bytes it has allocated.

#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

The program is not valid. Try replacing "int *p;" with "int *p = NULL;" and it will try to dereference a null pointer. This is because fun() makes a copy of the pointer, so when malloc() is called, it is setting the copied pointer to the memory location, not p. p is pointing to random memory before and after the call to fun(), and when you dereference it, it will crash. If you want to add memory to a pointer from a function, you need to pass the address of the pointer (ie. double pointer).

Please log in or register to report this question.

14.

What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int *numbers = (int*)calloc(4, sizeof(int));
   numbers[0] = 2;
   free(numbers);
   printf("Stored integers are ");
   printf("numbers[%d] = %d ", 0, numbers[0]);
   return 0;
}

 

Answer: A

Correct answer: Garbage value

The block is freed and then read through the same pointer. Once free has been called the memory belongs to the allocator again and may be reused or unmapped, so numbers[0] reads whatever happens to be there.

This is use after free, and it is undefined behaviour rather than merely unlucky - the program may print 2, may print rubbish, or may crash. It is particularly insidious because the old value often survives long enough to make the code look correct in testing.

Please log in or register to report this question.

15.

Point out the error in the following program.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *ptr;
    *ptr = (char)malloc(30);
    strcpy(ptr, "RAM");
    printf("%s", ptr);
    free(ptr);
    return 0;
}

 

Answer: B

Correct answer: Error: in *ptr = (char)malloc(30);

The line assigns to *ptr - a single character - rather than to ptr, and casts the returned address to (char), which throws away everything but its lowest byte.

It should read ptr = malloc(30);. The strcpy that follows is then writing through a pointer that holds no valid address, so it is the line most likely to crash - but the cause lies in the allocation line above it, which is what the question asks for.

Please log in or register to report this question.

16.

The call realloc(NULL, size) is equivalent to ________.

Answer: D

Correct answer: malloc(size)

The standard defines realloc() with a null first argument to behave exactly like malloc(). This lets a growing buffer be managed by a single realloc() call in a loop, with no special case for the first allocation.

Please log in or register to report this question.

Answer: C

Correct answer: It allocates a new block, copies the contents and frees the old one

When there is not enough adjacent free space, realloc() obtains a fresh block, copies the existing data across, releases the old block and returns the new address. This is why the returned pointer must always be captured rather than assumed unchanged.

Please log in or register to report this question.

18.

What does malloc() return if the requested memory cannot be allocated ?

Answer: C

Correct answer: A NULL pointer

On failure malloc() returns a null pointer, so every allocation should be tested before the pointer is used. Dereferencing the result without checking is one of the commonest causes of a crash in C.

Please log in or register to report this question.

Answer: D

Correct answer: Both a & b are true

Both statements describe malloc correctly. On success it returns a pointer to n bytes of uninitialised storage - the contents are whatever the allocator hands over, which is the difference from calloc.

On failure it returns NULL, and that is its only means of reporting failure. Checking the return value is therefore the caller's responsibility, and neglecting it is a common fault: the program dereferences a null pointer and crashes some distance from the real cause.

Please log in or register to report this question.

Answer: A

As defined Use of fflush(stdin) in C : fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream).

Please log in or register to report this question.

Loading…
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