Machine-translated by Google. Questions are authored in English.
Top

Discussion

Among 4 header files, which should be included to use the memory allocation functions like malloc(), calloc(), realloc() and free()?

  • A.#include<string.h>
  • B.#include<stdlib.h>
  • C.#include<memory.h>
  • D.Both b and c

Answer: B

Correct answer: #include<stdlib.h>

stdlib.h declares malloc, calloc, realloc and free. It is the only header of those offered that is required.

string.h declares memset and strcpy, which are often used alongside allocation but are not part of it. memory.h is a non-standard header on some systems and should not be relied upon. Omitting stdlib.h is particularly dangerous because an older compiler will assume malloc returns int and truncate the address.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.