Top

Memory Allocation

Answer: B

No answer description available for this question.

Enter details here

Answer: B

allocates the specified number of bytes and initializes them to zero.

Enter details here

13.

If malloc() successfully allocates memory it returns the number of bytes it has allocated.

# include
#include
void fun(int *a)
{
    a = (int*)malloc(sizeof(int));
}
int main()
{
    int *p;
    fun(p);
    *p = 6;
    printf("%dn",*p);
    return(0);
}

 

Answer: A

The program is not valid. Try replacing "int *p;" with "int *p = NULL;" and it will try to dereference a null pointer. This is because fun() makes a copy of the pointer, so when malloc() is called, it is setting the copied pointer to the memory location, not p. p is pointing to random memory before and after the call to fun(), and when you dereference it, it will crash. If you want to add memory to a pointer from a function, you need to pass the address of the pointer (ie. double pointer).

Enter details here

14.

What is the output of this program?

#include 
#include  
int main()
{
   int *numbers = (int*)calloc(4, sizeof(int));
   numbers[0] = 2;
   free(numbers);
   printf("Stored integers are ");
   printf("numbers[%d] = %d ", 0, numbers[0]);
   return 0;
}

 

Answer:

Enter details here

15.

Point out the error in the following program.

#include 
#include  
int main()
{
    char *ptr;
    *ptr = (char)malloc(30);
    strcpy(ptr, "RAM");
    printf("%s", ptr);
    free(ptr);
    return 0;
}

 

Answer: B

 

ptr = (char*)malloc(30);.

Enter details here

16.

Which of the following is an example of static memory allocation?

Answer: D

Array is an example of static memory allocation whereas linked list, queue and stack are examples for dynamic memory allocation.

Enter details here

Answer: C

 

Execution of the program using dynamic memory allocation is slower than that using static memory allocation. This is because in dynamic memory allocation, the memory has to be allocated during run time. This slows down the execution of the program.

Enter details here

Answer: C

Execution of the program using dynamic memory allocation is slower than that using static memory allocation. This is because in dynamic memory allocation, the memory has to be allocated during run time. This slows down the execution of the program.

Enter details here

Answer: D

None

Enter details here

Answer: A

As defined Use of fflush(stdin) in C : fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream).

Enter details here

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