1

According to http://en.cppreference.com/w/cpp/memory/c/malloc std::malloc returns a null ptr on failure

Is that a NULL pointer or a nullptr?

If it is nullptr, that implies there is a difference between std::malloc and the C malloc. Then the other question that follows, in that case, are there any other differences?

EDIT: This is not a duplicate as suggested in the comment. That explains what a nullptr is, clearly says they are different. The question is not asking about the difference between them.

Makketronix
  • 1,389
  • 1
  • 11
  • 31
  • 6
    There is no difference between `NULL` and `nullptr` *in this context* – Fatih BAKIR Oct 04 '17 at 03:28
  • Possible duplicate of [What exactly is nullptr?](https://stackoverflow.com/questions/1282295/what-exactly-is-nullptr) – user0042 Oct 04 '17 at 03:28
  • @Fatih, I appreciate your comment. However, it leaves us with more question. Why, who decides, ... – Makketronix Oct 04 '17 at 03:33
  • @Makketronix, who decides on what? `NULL` being equivalent with `nullptr` in terms of being the value of a pointer returned from a function? I'm going to say the standardization committee in that case – Fatih BAKIR Oct 04 '17 at 03:56
  • @Fatih, of course the committee decides... anyway, thanks for the effort. – Makketronix Oct 04 '17 at 03:59

3 Answers3

3

It returns NULL. From the N4296 draft of the C++ standard:

20.7.13 C library [c.malloc]

  1. Table 45 describes the header <cstdlib>.

    Table 45 — Header <cstdlib> synopsis:

    +------------+---------------+
    | Type       | Name(s)       |
    +------------+---------------+
    | Functions:   calloc malloc |
    |              free realloc  |
    +----------------------------+
    
  2. The contents are the same as the Standard C library header 11, with the following changes:
  3. The functions calloc(), malloc(), and realloc() do not attempt to allocate storage by calling ::operator new() (18.6).
  4. The function free() does not attempt to deallocate storage by calling ::operator delete().
  5. Storage allocated directly with malloc(), calloc(), or realloc() is implicitly declared reachable (see 3.7.4.3) on allocation, ceases to be declared reachable on deallocation, and need not cease to be declared reachable as the result of an undeclare_reachable() call.

As you can see, the C++ standard delegates to the C standard for the definition of malloc, and places no further restrictions on its return value or type. Since C has NULL but not nullptr, we can safely say that malloc returns NULL.

But all of this is moot since NULL == nullptr is always true, since they are both "null pointer constants" (a term which is clearly defined in 4.10 Pointer conversions [conv.ptr]). The two are equivalent.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • if I could, I would've given more, for showing the N4296. Teach a man how to fish... – Makketronix Oct 04 '17 at 03:40
  • 1
    @Makketronix Left sidebar right below the logo you will find a link to the current standard draft: https://isocpp.org/ Come to think of it, there's a lot of cool stuff at that site. – user4581301 Oct 04 '17 at 03:45
3

Neither.

The return type is std::malloc is void*. The return value of allocation failure is the null pointer value of type void*.

NULL is a pre-processor macro. A macro by itself has no type or value. This macro may be defined to be nullptr or 0. So we can't really say std::malloc return NULL. We can say it returns something equals to the value convertible from the value resolved from NULL.

Although nullptr is a "null pointer literal", it is not a pointer by itself. Its type is not traditional pointer type. Its type is, curiously, decltype(nullptr), which is not the return type of std::malloc. So std::malloc does not return nullptr.

1

std::malloc returns, first and foremost, a pointer. nullptr is not a pointer (that is literally the whole point of it). Therefore, std::malloc cannot return nullptr.

On failure, std::malloc will return a pointer value which is defined to be the "null pointer constant". nullptr is a non-pointer type that is implicitly convertible to the "null pointer constant", and is equality-comparable to any pointer value (returning equals if the pointer value is the "null pointer constant").

So if you test the return value against nullptr, it will test true if and only if std::malloc failed.

The macro NULL results (for C++) in the integer literal 0 (note: it can also result in nullptr, but pretty much no implementation does so). By C++'s rules, the integer literal 0 is implicit convertible to the null pointer constant, and is comparable to any pointer value (resulting in equals if the pointer value is the "null pointer constant").

So if you test the return value against NULL, it will test true if and only if std::malloc failed.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Ok, so malloc fails. What is the value of the register EAX/ RAX? Would that be 0, or would that be a memory location? – Makketronix Oct 04 '17 at 03:51
  • @Makketronix: The value is the "null pointer constant", as defined by the implementation. What value the implementation gives this value is not defined by the standard. But most platforms do pick a value that converts to a `uintptr_t` value of 0. – Nicol Bolas Oct 04 '17 at 03:53