2

I'm developing a user space application on Raspbian in C++11. ReSharper++ advises me to use nullptr instead of NULL in the following system call:

auto ret = timerfd_settime(this->_timer_fd, 0, &timeout, NULL);
assert(0 == ret);

I think that I should stay with NULL because this library is dedicated for system programming in C.

What is the best practice in this situation?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Krystian Sakowski
  • 1,613
  • 14
  • 20
  • 3
    `NULL` and `nullptr` are both valid null pointer constants in C++; it's just that `nullptr` is more type-safe in certain situations. If you have an "extern C" C linkage function that takes a pointer parameter (which may be null), it's perfectly fine to pass it `nullptr`. – The Paramagnetic Croissant Sep 05 '15 at 20:24
  • Thanks, that is the answer to my question. – Krystian Sakowski Sep 05 '15 at 20:31

2 Answers2

2

Both NULL and nullptr will work fine. However, since this is syspro in C, I would go with NULL, because it's what I see more often (if not always) in that field.

If you check the reference of your system call, you will see that everywhere NULL is mentioned, not nullptr, something which you should expect (if not make sure you 'll read the following answer).

Check also NULL vs nullptr.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
2

I would stay with nullptr because the intention is more clear.

There is a implicit conversion of nullptr to pointer for any type so you are definitely safe.

cppreference.com:

The keyword nullptr denotes the pointer literal. It is a prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type.

CyberGuy
  • 2,783
  • 1
  • 21
  • 31