简体   繁体   中英

Does std::malloc return NULL or nullptr on failure

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.

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM