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

Memory Allocation Questions and Answers

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.

62.

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.

64.

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.

65.

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.

66.

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.

67.

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.

68.

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.

69.

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.

70.

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.

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