From e6baf3d2fb33ff980611fcfd166842489615c8d4 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Mon, 12 Oct 2020 10:10:07 +0200 Subject: [PATCH] C-Allocators: Mention `reallocarray` for safe array allocation Signed-off-by: Daiki Ueno --- modules/ROOT/pages/programming-languages/C-Allocators.adoc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/ROOT/pages/programming-languages/C-Allocators.adoc b/modules/ROOT/pages/programming-languages/C-Allocators.adoc index 1ef22cd..6bb137a 100644 --- a/modules/ROOT/pages/programming-languages/C-Allocators.adoc +++ b/modules/ROOT/pages/programming-languages/C-Allocators.adoc @@ -118,6 +118,12 @@ to allocate an array of `n` elements of type greater than `((size_t) -1) / sizeof(T)`. See <>. +GNU libc provides a dedicated function `reallocarray` that allocates +an array with those checks performed internally. However, care must +be taken if portability is important: while the interface originated +in OpenBSD and has been adopted in many other platforms, NetBSD +exposes an incompatible behavior with the same interface. + [[sect-Defensive_Coding-C-Allocators-Custom]] === Custom Memory Allocators