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

Memory Allocation Questions and Answers

1.

What function should be used to free the memory allocated by calloc() ?

Answer: C

Correct answer: free();

Memory from calloc is released by free, exactly as memory from malloc or realloc is. There is one deallocation function for all three.

That is possible because all three obtain their storage from the same allocator; the difference between them lies in how the block is sized and whether it is zeroed, not in where it comes from. The other names offered are not part of standard C.

Please log in or register to report this question.

2.

Can I increase the size of dynamically allocated array?

Answer: A

Correct answer: Yes

realloc resizes an existing block, preserving its contents up to the smaller of the old and new sizes. It may extend the block in place, or move it elsewhere and copy the data, returning the address either way.

Two cautions follow. The returned pointer must be used from then on, since the old one may now be invalid; and it should be assigned to a temporary first, because realloc returns null on failure and assigning that directly over the original pointer loses the block entirely.

Please log in or register to report this question.

Answer: C

Correct answer: i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap

Each of the three lives in a different region. i is a global with no initialiser, so it has static storage duration and is zero-initialised - such objects go in the BSS, which the loader clears rather than reading from the file.

j is an automatic variable inside main, so it is on the stack. The object k points to comes from malloc and so lives on the heap; note that k itself, the pointer, is on the stack alongside j.

Please log in or register to report this question.

4.

Among 4 header files, which should be included to use the memory allocation functions like malloc(), calloc(), realloc() and free()?

Answer: B

Correct answer: #include<stdlib.h>

stdlib.h declares malloc, calloc, realloc and free. It is the only header of those offered that is required.

string.h declares memset and strcpy, which are often used alongside allocation but are not part of it. memory.h is a non-standard header on some systems and should not be relied upon. Omitting stdlib.h is particularly dangerous because an older compiler will assume malloc returns int and truncate the address.

Please log in or register to report this question.

Answer: D

Correct answer: undefined behaviour

A double free corrupts the allocator’s internal bookkeeping and its consequences are undefined, ranging from a silent fault to an exploitable crash. Assigning NULL to the pointer after the first free() makes any second call harmless.

Please log in or register to report this question.

6.

Can I increase the size of dynamically allocated array?

Answer: A

Correct answer: Yes

realloc resizes an existing block, preserving its contents up to the smaller of the old and new sizes. It may extend the block in place, or move it elsewhere and copy the data, returning the address either way.

Two cautions follow. The returned pointer must be used from then on, since the old one may now be invalid; and it should be assigned to a temporary first, because realloc returns null on failure and assigning that directly over the original pointer loses the block entirely.

Please log in or register to report this question.

7.

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.

8.

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.

10.

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.

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