From e435151a0cb9485a0f5567b3c855ee030216ff0d Mon Sep 17 00:00:00 2001 From: Huzaifa Sidhpurwala Date: Feb 11 2022 12:34:37 +0000 Subject: Merge #21 `Do not use alloca inside a loop` --- diff --git a/modules/ROOT/pages/programming-languages/C.adoc b/modules/ROOT/pages/programming-languages/C.adoc index ce92257..6dcd443 100644 --- a/modules/ROOT/pages/programming-languages/C.adoc +++ b/modules/ROOT/pages/programming-languages/C.adoc @@ -676,6 +676,14 @@ Otherwise, call `malloc`. When exiting the function, check if `malloc` had been called, and free the buffer as needed. +Remember that memory allocated on the stack through `alloca` +is released at the end of the function and not at the end of +the block where it is defined, thus it is reccommended to not +call `alloca` inside a loop. In this regard, VLA behaves better, +considering the memory allocated with VLA is released at the end +of the block that defines them. Do not mix VLA and `alloca` though, +otherwise this behaviour is not guaranteed for VLA either! + [[sect-Defensive_Coding-C-Allocators-Arrays]] === Array Allocation