Which of the following is true?
Answer: B
No answer description available for this question.
Enter details here
Why is calloc() function used for?
Answer: B
allocates the specified number of bytes and initializes them to zero.
Enter details here
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
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
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
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
Choose the statement which is incorrect with respect to dynamic memory allocation.
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
Choose the statement which is incorrect with respect to dynamic memory allocation.
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
Why to use fflush() library function?
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