Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>C does not support zero-sized objects, but the argument to <code>malloc()</code> is of type <code>size_t</code>, and there's no good way to prevent a program from calling <code>malloc(0)</code>. It may not be the programmer's intent, but it's not necessarily literally <code>malloc(0)</code>; it's more likely to be <code>malloc(count)</code>, where <code>count</code> is the result of some computation.</p> <p>As for the standard permitting two different behaviors, that's simply because existing implementations (at the time the original standard was being written) did different things, and the authors wanted to avoid breaking existing code. Such code was arguably already broken, or at least non-portable, but by permitting either behavior a program that made assumptions about how <code>malloc(0)</code> behaves could continue to work <em>on the system for which it was written</em>.</p> <p>If you're looking for a coherent explanation, you're not going to find one. If C were being designed from scratch today, the behavior of <code>malloc(0)</code> almost certainly would have been nailed down, one way or the other. Either that, or the behavior would have been made undefined, but making it implementation-defined means that code doesn't have to check quite as carefully that it's not passing zero to <code>malloc()</code>.</p> <p>And in fact the committee's decision is documented in the <a href="http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf" rel="nofollow">C99 Rationale</a>, section 7.20.3, pages 160-161.</p> <p>It does mean that:</p> <pre><code>void *ptr = malloc(0); free(ptr); </code></pre> <p>will work correctly; <code>free()</code> does nothing if its argument is a null pointer.</p> <p>What can you do with the result of <code>malloc(0)</code>? Well, if <code>malloc(1024)</code> is successful, you can store 1024 bytes in the allocated space. You can store <em>no</em> bytes in the space allocated by <code>malloc(0)</code> -- which is exactly what you asked for.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COI absoloutly agree to that what you said so far, but that isn't cracking, why the comittee didn't just say "OK, malloc(0)(or ofc malloc(count), too. Just said 0 size, to make it more clearly) can't return anymore an non-null pointer. I mean the only code, which could get broken, is code that was before going to work in invalid memory. With such isntructions the developer just should blame him self. But did I get it right, that malloc is used in other languages as c/c++ which are handling that case? (or did u mention c++ is supporting zero sized blocks?I'm not that conform with c++)
      singulars
    2. CO@Zaibis: `malloc` works the same way in C and C++, but C++ code normally uses `new` instead. Prior to the C standard, there were C implementations where `malloc(0)` would return a unique non-null pointer, and presumably there were programs that depended on that behavior; for example, such pointers are unique, which could be useful. Those implementations could continue to work the same way, and programs that depended on that behavior could continue to work *on those implementations*. I'm not convinced that the committee made the best decision; I'm sure political compromise was involved.
      singulars
    3. CO@KeithThompson: If there were nothing but `malloc` and `free`, zero-byte allocations probably wouldn't make any sense, but there's also `realloc` to consider. If a buffer gets resized repeatedly, and the requested size is sometimes zero, the system shouldn't be overly eager to give away the memory immediately following a shrunken buffer to anything other than its former owner, but if `realloc` with a size of zero returns null, the former owner of the buffer will have no way to ask for it back.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload