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

Memory Allocation Questions and Answers

71.

What is the output of this program?

#include <stdio.h>
void main()
{
   int *ptr = (int *)malloc(sizeof(int));
   *ptr = 10;
   free(ptr);
   p = 5;
   printf("%d", ptr);
}

 

Answer: B

 

free() will not deallocate the memory it just to delete all data's allocated to a variable (ptr). In this program, first integer value 10 is assigned to the pointer variable *ptr and then its data is deleted using free(ptr) and then new integer value 5 is assigned to the variable ptr and then 5 is outputted.

Please log in or register to report this question.

72.

What is the problem with following code?

#include <stdio.h>
int main()
{
    int *p = (int *)malloc(sizeof(int));
 
    p = NULL;
 
    free(p);
}

 

Answer: B

Correct answer: Memory Leak

The program allocates a block, then overwrites the only pointer to it with NULL. Nothing now holds the block's address, so it can never be freed - that is a memory leak.

Calling free(NULL) is explicitly defined to do nothing, so the last line is harmless and there is no crash and no compiler error. Nor is there a dangling pointer, which would require the pointer still to refer to freed memory. The fault is that the block was lost before it could be released.

Please log in or register to report this question.

73.

Which statment is true about the given code ?

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *a[5];
    a = (int*) malloc(sizeof(int)*5);
    free(a);
    return 0;
}

 

Answer: B

Correct answer: Error: We cannot store address of allocated memory in a

int *a[5] declares an array of five pointers. An array name is not a modifiable lvalue, so a = ... is rejected by the compiler - nothing can be assigned to it.

The declaration the code seems to intend is int *a;, a single pointer, which could then hold the allocated address. The distinction between an array of pointers and a pointer is exactly the kind of declaration that repays reading from the inside out.

Please log in or register to report this question.

74.

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.

75.

How will you free the memory allocated by the following program?

#include <stdio.h>
#include <stdlib.h>
#define MAXROW 2
#define MAXCOL 3
int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}

 

Answer: D

Correct answer: free(p);

Whatever malloc returned is what free must be given, so a single free(p) releases the block of row pointers.

Note what the program has actually allocated: only the array of MAXROW pointers, not the rows themselves. Had the loop allocating each row been present, every one of those blocks would need freeing individually before free(p) - releasing the outer array first would lose the addresses of the rows and leak them all.

Please log in or register to report this question.

76.

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.

Answer: B

Correct answer: False

malloc always returns void *, whatever the memory will be used for. It cannot do otherwise: the only argument it receives is a number of bytes, so it has no knowledge of the intended type.

The conversion to float * or double * happens at the assignment, implicitly in C. What malloc does guarantee is alignment suitable 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.

77.

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.

78.

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 = 2.5f;
  printf("%f", ptr->f);
  return 0;
}

 

Answer: B

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 2.5f and then outputted as 2.500000.

Please log in or register to report this question.

Answer: B

Correct answer: During Preprocessing the Code #include replaces by stdio.h

#include is a preprocessor directive, and the preprocessor runs before the compiler sees anything. It replaces the directive with the entire text of the named file, producing a single translation unit.

What is inserted is declarations, not code: the header tells the compiler the types and signatures of the library functions, while the functions themselves live in the library and are joined at link time. Confusing the two is what makes a missing library a puzzling error, since the header alone compiles perfectly well.

Please log in or register to report this question.

80.

The most appropriate matching for the following pairs

X: m=malloc(5); m= NULL;        1: using dangling pointers
Y: free(n); n->value=5;         2: using uninitialized pointers
Z: char *p; *p = 'a';           3. lost memory is:

Answer: D

Correct answer: X-3 Y-1 Z-2

Take each line in turn. X allocates memory and then overwrites the only pointer to it with NULL, so the block can never be freed - that is lost memory, a leak, giving X-3.

Y frees the block and then writes through the same pointer, which now refers to memory the allocator has reclaimed - a dangling pointer, giving Y-1. Z declares a pointer without ever assigning it an address and writes through it, which is an uninitialised pointer, giving Z-2. The three are the classic pointer faults.

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