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

Memory Allocation Questions and Answers

Answer: A

Correct answer: To flush all streams and specified streams

fflush(stream) writes out any buffered data for that stream, and fflush(NULL) flushes every output stream the program has open. Both forms are provided, which is what the option describes.

Its normal use is to force output to appear when it would otherwise sit in the buffer - before a prompt, or before a crash. Note that fflush on an input stream is undefined behaviour in standard C, despite being widely and wrongly used to discard unread input.

Please log in or register to report this question.

52.

Point out the error of the following code −

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

 

Answer: B

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

The asterisk is in the wrong place. ptr is a char *, so *ptr is a single char, and the line attempts to assign a pointer to it. The types are incompatible and the address is discarded even if the compiler is persuaded to accept it.

The line should read ptr = malloc(30);. Everything after it then depends on this error: strcpy writes through a pointer that was never given a valid address, which is undefined behaviour.

Please log in or register to report this question.

53.

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.

54.

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.

55.

What will be the output of the program (16-bit platform)?

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

int main()
{
    int *p;
    p = (int *)malloc(20);
    printf("%d\n", sizeof(p));
    free(p);
    return 0;
}

 

Answer: B

Correct answer: 2

sizeof(p) gives the size of the pointer, not of the 20 bytes it points to. On the 16-bit platform the question specifies, a near pointer holds a 16-bit offset and occupies 2 bytes.

The same expression gives 4 on a 32-bit build and 8 on a 64-bit one, which is why the platform has to be stated for the question to have an answer. There is no portable way to ask how large an allocated block is; a program that needs to know must record the size itself.

Please log in or register to report this question.

56.

On freeing a dynamic memory, if the pointer value is not modified, then the pointer points to?

Answer: C

Correct answer: The same deallocated memory location

free receives the pointer by value and cannot alter the caller's copy, so the bits in the pointer are exactly what they were. It still holds the address of the block - which now belongs to the allocator again.

This is a dangling pointer, and it is dangerous precisely because it looks valid and may appear to work while the freed block is untouched. Setting the pointer to NULL immediately after freeing is the standard discipline, since a null dereference fails immediately and visibly.

Please log in or register to report this question.

Answer: A

Correct answer: Dynamic memory allocation

malloc and calloc obtain storage at run time from the heap, in a quantity that may be computed rather than fixed in the source. That is what dynamic allocation means.

Static allocation is settled by the compiler: globals and static variables are laid out before the program runs, and automatic variables are allocated by the function prologue. Neither requires a library call, which is the practical signal that they are a different mechanism. Dynamically allocated storage must be released with free; static storage never is.

Please log in or register to report this question.

58.

malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's. A.

Answer: B

Correct answer: FALSE

malloc returns void * regardless of what will be stored in the block. It has no way of knowing: the only argument it receives is a number of bytes.

The conversion to float * or double * happens at the assignment, implicitly in C. What malloc does guarantee is that the block is suitably aligned for any type, which is what makes that later conversion safe whatever the caller has in mind.

Please log in or register to report this question.

Answer: D

Correct answer: Both b & c

free accepts the pointer returned by malloc, by calloc or by realloc - one deallocation function serves all three, because all three draw on the same allocator.

What it will not accept is anything else: an address part-way into a block, an address of an automatic or static object, or a pointer already freed. Each of those is undefined behaviour, and a double free in particular corrupts the allocator's own bookkeeping.

Please log in or register to report this question.

60.

Which header file should be included to use functions like malloc() and calloc()?

Answer: B

Correct answer: stdlib.h

stdlib.h declares malloc, calloc, realloc and free, together with size_t.

Omitting it is more dangerous than it looks. Without the prototype an older compiler assumes malloc returns int, and where a pointer is wider than an int the address is silently truncated. A cast on the result hides exactly the warning that would have revealed it, which is the main argument against casting in C.

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