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

Memory Allocation Questions and Answers

61.

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.

62.

Consider the following three C functions :

[PI] int * g (void) 
{ 
  int x= 10; 
  return (&x); 
}  
  
[P2] int * g (void) 
{ 
  int * px; 
  *px= 10; 
  return px; 
} 
  
[P3] int *g (void) 
{ 
  int *px; 
  px = (int *) malloc (sizeof(int)); 
  *px= 10; 
  return px; 
}

Which of the above three functions are likely to cause problems with pointers?

Answer: C

Correct answer: Only P1 and P2

P1 returns the address of a local variable. Its lifetime ends when the function returns, so the caller receives a pointer to storage that no longer belongs to it - a dangling pointer.

P2 never assigns px an address at all, then writes through it, which is an uninitialised pointer and undefined behaviour. P3 is correct: it obtains storage from malloc, which has allocated lifetime and survives the return. The caller must free it, but that is a responsibility rather than a fault.

Please log in or register to report this question.

63.

What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int i, numbers[1];
  numbers[0] = 15;
  free(numbers);
  printf("Stored integers are ");
  printf("numbers[%d] = %d ", 0, numbers[0]);
  return 0;
}

 

Answer: A

he memory allocation function free() will not free or delete the content in the memory location, because free() can only free or delete the content in the memory, who's memory is allocated either by malloc() or calloc().

Please log in or register to report this question.

64.

Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program?

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

int main()
{
    struct ex
    {
        int i;
        float j;
        char *s
    };
    struct ex *p;
    p = (struct ex *)malloc(sizeof(struct ex));
    p->s = (char*)malloc(20);
    return 0;
}

 

Answer: B

Correct answer: free(p->s); , free(p);

The order matters. p->s is reachable only through p, so freeing p first would destroy the only record of the inner block's address and leak it permanently.

Releasing the inner allocation before the outer one is the general rule for any nested structure, and it means deallocation runs in the reverse order of allocation. The same reasoning applies to a two-dimensional array built as an array of row pointers: free every row, then free the array of pointers.

Please log in or register to report this question.

65.

What is the return type of malloc() or calloc()

Answer: A

Correct answer: void *

Both return void *, a pointer to memory of no particular type. That is what allows one allocator to serve every type in the language: the caller assigns the result to a pointer of the type it wants, and the conversion happens implicitly.

In C the cast is therefore unnecessary, and writing one can conceal a missing stdlib.h - without the prototype the compiler assumes a return type of int, and the cast silences the warning that would have exposed it. In C++ the cast is required, which is why it is so often seen.

Please log in or register to report this question.

66.

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.

67.

What will be the output of the program?

#include <stdio.h>
#include <string.h>

int main()
{
    char *s;
    char *fun();
    s = fun();
    printf("%s\n", s);
    return 0;
}
char *fun()
{
    char buffer[30];
    strcpy(buffer, "RAM");
    return (buffer);
}

 

Answer: B

Correct answer: Garbage value

fun returns the address of buffer, an automatic array whose lifetime ends when the function returns. The caller therefore holds a pointer to storage that has been reclaimed.

Reading through it is undefined behaviour. The text may survive briefly, but the call to printf itself uses the stack and is quite likely to overwrite it, so garbage is the usual result. Returning a pointer to a local is one of the classic C faults; the fix is to allocate the buffer with malloc or to have the caller supply it.

Please log in or register to report this question.

68.

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.

69.

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.

70.

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.

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