0

C standard (e.g. C99) tells that malloc(0) returns "a null pointer or a unique pointer that can be successfully passed to free()". Which of the two, is implementation-defined. (This has been asked on StackOverflow many times, e.g. 1, 2, 3, 4.)

My question is: how does GCC define it?

Community
  • 1
  • 1
Konstantin Shemyak
  • 2,369
  • 5
  • 21
  • 41

1 Answers1

5

GCC doesn't define it at all. GCC uses the malloc() implementation provided by the standard library. On OS X this will be libSystem, on Linux this will usually be glibc or eglibc, etc. These libraries are open source, so you can browse the source code.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • @Dietrich: Thank you! http://bazaar.launchpad.net/~vcs-imports/glibc/master/view/head:/malloc/malloc.c#L117 says that "Even a request for zero bytes (i.e., malloc(0)) returns a pointer to something of the minimum allocatable size." – Konstantin Shemyak Jan 14 '15 at 19:38