Discussion
How will you free the memory allocated by the following program?
#include <stdio.h>
#include <stdlib.h>
#define MAXROW 2
#define MAXCOL 3
int main()
{
int **p, i, j;
p = (int **) malloc(MAXROW * sizeof(int*));
return 0;
}
Answer: D
Correct answer: free(p);
Whatever malloc returned is what free must be given, so a single free(p) releases the block of row pointers.
Note what the program has actually allocated: only the array of MAXROW pointers, not the rows themselves. Had the loop allocating each row been present, every one of those blocks would need freeing individually before free(p) - releasing the outer array first would lose the addresses of the rows and leak them all.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development