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

Memory Allocation Questions and Answers

51.

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.

52.

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.

53.

Where does the uninitialized data gets stored in memory?

Answer: C

Correct answer: BSS- Block started by symbol

The BSS holds objects with static storage duration that have no explicit initialiser, and which are therefore zero-initialised before the program starts.

Keeping them separate saves space in the executable: the file need only record how many bytes to reserve, and the loader clears them, rather than storing a long run of zeros on disk. Initialised globals go in the data segment proper, where their values must actually be stored, and the code segment holds the instructions.

Please log in or register to report this question.

54.

malloc() returns a NULL if it fails to allocate the requested memory.

Answer: A

Correct answer: TRUE

When malloc cannot satisfy a request it returns a null pointer. That is the only way it reports failure - there is no error code and nothing is set beyond, on most systems, errno.

Checking the return value is therefore the caller's responsibility, and omitting the check is a common fault: the program proceeds to dereference a null pointer and crashes at a line some distance from the real cause. Note that malloc(0) may legitimately return either null or a unique pointer.

Please log in or register to report this question.

55.

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

Answer: B

Correct answer: FALSE

malloc returns a pointer to the allocated block, not a count of bytes. On success it is the address of the storage; on failure it is null.

The number of bytes is what the caller supplied as the argument, so returning it would convey nothing. There is in fact no portable way to ask how large an allocated block is once it has been made, which is why programs that need to know must record the size themselves alongside the pointer.

Please log in or register to report this question.

56.

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.

57.

Can we increase the size of statically allocated array?

Answer: B

Correct answer: No

A statically allocated array has its size fixed at compile time, in the stack frame or the data segment, and there is no mechanism to change it while the program runs.

The only way to get a larger array is to allocate a new one dynamically and copy the contents across - which is precisely what realloc does for heap blocks. This inflexibility is the central reason dynamic allocation exists, and it is why growable structures are always built on the heap.

Please log in or register to report this question.

58.

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.

59.

What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
 struct test
 {
   int i;
   float f;
   char c;
 };
 struct test *ptr;
 ptr = (struct test *)malloc(sizeof(struct test));
 ptr ->f = 5.5f;
 printf("%f", ptr->f);
 return 0;
}

 

Answer: C

Here test is a structure name which consist of three variables of three different data types. ptr is the pointer variable, for which the memory is allocated dynamically by using malloc() of function and then it is pointed towards the structure floating variable f. Now f is initialized with a value 5.5f and then outputted as 5.500000.

Please log in or register to report this question.

60.

If p is a pointer to a block allocated with malloc(), what does sizeof(p) give ?

Answer: D

Correct answer: The size of a pointer on that platform

sizeof is resolved at compile time and knows nothing of what malloc() did at run time, so it reports the size of the pointer itself, typically 8 bytes on a 64-bit system. C provides no portable way to recover the size of an allocated block, so the program must remember it.

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