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

Discussion

Which of the following statement is correct prototype of the malloc() function in c ?

  • A.int* malloc(int);
  • B.char* malloc(char);
  • C.unsigned int* malloc(unsigned int);
  • D.void* malloc(size_t);

Answer: D

Correct answer: void* malloc(size_t);

The prototype takes a size_t - an unsigned type large enough to hold the size of any object - and returns void *.

Both halves matter. The void * return is what allows one function to serve every type, and size_t rather than int is what allows allocations larger than an int can express on a 64-bit machine. It also means a negative size argument is converted to a very large positive one, so the allocation simply fails rather than misbehaving.

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

Post your comment

Please log in or register to post a comment.