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

Memory Allocation Questions and Answers

Answer: B

Correct answer: enough total free memory exists but not as one contiguous block

Repeated allocation and freeing of blocks of differing sizes leaves free space scattered between live blocks. A request may then fail even though the total free space would suffice, because no single gap is large enough.

Please log in or register to report this question.

Answer: B

Correct answer: Nothing happens; it is safe

The C standard requires free(NULL) to do nothing at all. This is why cleanup code can call free() on a pointer without first testing it, provided the pointer was set to NULL when no allocation was made.

Please log in or register to report this question.

83.

What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int *j = (int*)malloc(4 * sizeof(int));
   *j = 15;
   free(j);
   printf("%d", *j);
   return 0;
}

 

Answer: B

In this program, a pointer variable *j is declared and its memory space is allocated using malloc() and then an integer value 15 is set to a pointer variable *j. Now free(j) is used to free or delete the memory space allocated to the pointer variable *j using malloc(). While freeing the memory space allocated using malloc(), all allocated space will be deleted and by default the deleted space will be denoted by some garbage value and is outputted.

Please log in or register to report this question.

84.

During preprocessing, the code #include gets replaced by the contents of the file stdio.h. Which is true?

Answer: B

Correct answer: Yes

The statement is correct: #include is handled by the preprocessor, which runs before compilation and replaces the directive with the entire contents of the named file.

The result is a single translation unit containing the program text and every header it pulled in. What the header supplies is declarations - types and function signatures - so that the compiler can check calls. The functions themselves live in the library and are joined at link time, which is a later and quite separate stage.

Please log in or register to report this question.

85.

Assume integer is 2 bytes wide. What will be the output of the following code?

#include <stdio.h>
#include <stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int (*p)[MAXCOL];
    p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
    printf("%d, %d\n", sizeof(p), sizeof(*p));
    return 0;
}

 

Answer: A

Correct answer: 2, 8

Two different quantities are being measured. sizeof(p) is the size of the pointer itself, which on the 16-bit platform assumed here is 2 bytes.

sizeof(*p) is the size of what it points to - an array of MAXCOL integers:

\[ 4 \times 2 = 8\ \mathrm{bytes} \]

The pair illustrates why sizeof on a pointer almost never gives what a beginner expects: it reports the size of the address, and the pointed-to type must be dereferenced to be measured.

Please log in or register to report this question.

86.

What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int *p;
  p = (int *)malloc(40);
  printf("%d", sizeof(p));
  free(p);
  return 0;
}

 

Answer: C

Since p is the pointer variable of integer type, thus the size of integer type is 4.

Please log in or register to report this question.

87.

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.

88.

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.

89.

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.

90.

Specify the 2 library functions to dynamically allocate memory?

Answer: C

Correct answer: malloc() and calloc()

malloc and calloc are the two allocation functions in standard C. malloc takes a single total size and leaves the block uninitialised; calloc takes a count and an element size and zeroes the result.

The standard library also provides realloc to resize an existing block and free to release one, so four functions in all - but only two of them allocate fresh storage. The others named in the options are not standard C at all.

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