Govt. Exams
Entrance Exams
Advertisement
Topics in C Programming
What happens when you try to access memory after calling free()?
Correct Answer:
B. Use-after-free error (undefined behavior)
EXPLANATION
Accessing freed memory results in undefined behavior and is a critical bug. The memory may be reallocated for other purposes.
What is a memory leak in C?
Correct Answer:
B. Allocated memory that is never freed
EXPLANATION
A memory leak occurs when allocated memory is not freed, consuming system resources without releasing them back.
What will be the output of the following code?
int *ptr = malloc(sizeof(int));
if(ptr == NULL) printf("Failed");
else printf("Success");
int *ptr = malloc(sizeof(int));
if(ptr == NULL) printf("Failed");
else printf("Success");
Correct Answer:
B. Success
EXPLANATION
In typical scenarios, malloc() successfully allocates memory and returns non-NULL pointer, printing 'Success'. NULL check is good practice for error handling.