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

Memory Allocation Questions and Answers

11.

When we dynamically allocate memory is there any way to free memory during run time?

Answer: A

Correct answer: Yes

free returns a block to the allocator at any point during execution, and the storage becomes available for later allocations. That is the whole point of the heap: its lifetimes are under the program's control rather than tied to function entry and exit.

The control brings responsibility. Failing to free leaks memory, freeing twice corrupts the allocator's bookkeeping, and using a pointer after freeing it is undefined behaviour - the three faults that dominate debugging in C.

Please log in or register to report this question.

12.

What is the Error of this 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);

Two things are wrong on that one line. *ptr is a single char, not a pointer, so the assignment target is wrong; and the cast to (char) converts the returned address to a single character, discarding all but its lowest byte.

The line should read ptr = malloc(30);. Everything downstream then depends on it: strcpy writes through a pointer that never received a valid address, which is undefined behaviour and will usually fault.

Please log in or register to report this question.

Answer: D

Correct answer: All of the above

All three statements hold. calloc zeroes what it returns while malloc leaves arbitrary data; malloc followed by memset reproduces that effect; and both return void *.

One difference the list does not mention is worth knowing: calloc multiplies its two arguments internally and is required to detect overflow in that product, whereas a hand-written malloc(m * n) can wrap silently and allocate far too little - a well-known source of security bugs.

Please log in or register to report this question.

14.

How many bytes of memory will the following code reserve?

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

int main()
{
    int *p;
    p = (int *)malloc(256 * 256);
    if(p == NULL)
        printf("Allocation failed");
    return 0;
}

 

Answer: B

Hence 256*256 = 65536 is passed to malloc() function which can allocate upto 65535. So the memory allocation will be failed in 16 bit platform (Turbo C in DOS).

If you compile the same program in 32 bit platform like Linux (GCC Compiler) it may allocate the required memory

Please log in or register to report this question.

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.

17.

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.

18.

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.

19.

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.

20.

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.

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