Discussion
Which of the following is true?
Answer: B
Correct answer: "ptr = calloc(m, n)" is equivalent to following ptr = malloc(m * n); memset(ptr, 0, m * n);
calloc(m, n) does two things: it allocates \( m \times n \) bytes, and it sets every one of them to zero. Reproducing it with malloc therefore needs both steps - the allocation and the memset.
The option offering malloc(m * n) alone gets the size right but leaves the block holding arbitrary data. The two options using m or n alone allocate far too little, since calloc's two arguments are multiplied. That multiplication is also where calloc checks for overflow, which a hand-written product does not.
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