Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

77%
+5 −0
Q&A Should I check if pointer parameters are null pointers?

Whether null pointer checking should be the caller's responsibility or the callee is debatable and probably a matter of opinion, local convention, sometimes logical choices but in all cases should ...

posted 2y ago by chqrlie‭  ·  edited 2y ago by Dirk Herrmann‭

Answer
#2: Post edited by user avatar Dirk Herrmann‭ · 2022-04-06T09:56:12Z (over 2 years ago)
Small typo
  • Whether null pointer checking should be the caller's responsibility or the callee is debatable and probably a matter of opinion, local convention, sometimes logical choices but in all cases should be documented explicitly.
  • Consider for example `memcpy()`: it is the caller's responsibility, the behavior is undefined if either pointer is invalid unless the size is `0` and if the destination and source ranges overlap. There are good reasons for this: `memcpy` is meant to be as efficient as possible so not extra tests are needed. This allows compilers to generate optimal inline code for `memcpy` calls, sometimes as small as a single instruction.
  • Conversely, consider `free()`: this is function specifically accepts a null pointer argument, so no test is required at the call site to free any allocated data, whether it was allocated successfully or not as long as the pointers were initialized to `NULL`.
  • The original rationale of the C language was to favor performance at the cost of increased programmer's responsibility. Note that it is always the programmer's responsibility to pass valid pointers along with enough information for the callee to access only relevant data via the pointer. Small functions can be more efficient without null checking, larger ones can have extra checks at minimal cost (quite negligible on CPUs with branch prediction). Handling null pointers gracefully is more a question of quality of implementation in this case (eg: `printf("%s", (char *)NULL);`).
  • Another sound approach is too add assertions in the function prologue to perform null checking only in DEBUG builds. Such functions should be documented as taking non null pointers and purposely adorned in the source code if annotations are available to specify this restriction (via `__attribute__((__nonnull__))`, `static` size specifications, or similar annotations)
  • Whether null pointer checking should be the caller's responsibility or the callee is debatable and probably a matter of opinion, local convention, sometimes logical choices but in all cases should be documented explicitly.
  • Consider for example `memcpy()`: it is the caller's responsibility, the behavior is undefined if either pointer is invalid unless the size is `0` and if the destination and source ranges overlap. There are good reasons for this: `memcpy` is meant to be as efficient as possible so not extra tests are needed. This allows compilers to generate optimal inline code for `memcpy` calls, sometimes as small as a single instruction.
  • Conversely, consider `free()`: this is function specifically accepts a null pointer argument, so no test is required at the call site to free any allocated data, whether it was allocated successfully or not as long as the pointers were initialized to `NULL`.
  • The original rationale of the C language was to favor performance at the cost of increased programmer's responsibility. Note that it is always the programmer's responsibility to pass valid pointers along with enough information for the callee to access only relevant data via the pointer. Small functions can be more efficient without null checking, larger ones can have extra checks at minimal cost (quite negligible on CPUs with branch prediction). Handling null pointers gracefully is more a question of quality of implementation in this case (eg: `printf("%s", (char *)NULL);`).
  • Another sound approach is to add assertions in the function prologue to perform null checking only in DEBUG builds. Such functions should be documented as taking non null pointers and purposely adorned in the source code if annotations are available to specify this restriction (via `__attribute__((__nonnull__))`, `static` size specifications, or similar annotations)
#1: Initial revision by user avatar chqrlie‭ · 2022-04-05T13:24:34Z (over 2 years ago)
Whether null pointer checking should be the caller's responsibility or the callee is debatable and probably a matter of opinion, local convention, sometimes logical choices but in all cases should be documented explicitly.

Consider for example `memcpy()`: it is the caller's responsibility, the behavior is undefined if either pointer is invalid unless the size is `0` and if the destination and source ranges overlap. There are good reasons for this: `memcpy` is meant to be as efficient as possible so not extra tests are needed. This allows compilers to generate optimal inline code for `memcpy` calls, sometimes as small as a single instruction.

Conversely, consider `free()`: this is function specifically accepts a null pointer argument, so no test is required at the call site to free any allocated data, whether it was allocated successfully or not as long as the pointers were initialized to `NULL`.

The original rationale of the C language was to favor performance at the cost of increased programmer's responsibility. Note that it is always the programmer's responsibility to pass valid pointers along with enough information for the callee to access only relevant data via the pointer. Small functions can be more efficient without null checking, larger ones can have extra checks at minimal cost (quite negligible on CPUs with branch prediction). Handling null pointers gracefully is more a question of quality of implementation in this case (eg: `printf("%s", (char *)NULL);`).

Another sound approach is too add assertions in the function prologue to perform null checking only in DEBUG builds. Such functions should be documented as taking non null pointers and purposely adorned in the source code if annotations are available to specify this restriction (via `__attribute__((__nonnull__))`, `static` size specifications, or similar annotations)