Advantages of using malloc here?

You should basically never, ever use VLAs outside of VLA types, and you happen to have done it in an exceptionally unsafe fashion.

(Higher-level, if you aren’t sure you have C99 support, then all array sizes must be a positive compile-time constant and you have to pack your local variable definitions at the top of the block. Also things like declaring a variable in the heas of a for loop, variadic macro parameters, <stdint.h>, and <stdbool.h>, although <stdint.h> also came up with <inttypes.h> through POSIX.1-2001 IIRC. If you aren’t allowed to use C99, throw -std=c99 -pedantic -Wall -Wextra onto your CPPFLAGS to kvetch about non-C89 stuff, or do this to detect it:

#if (__STDC_VERSION__+0) < 199901L
#error "this code requires a C99 mode/compiler"
#include "./<<<STOP>>>/."
#endif

It’s a good idea to do that so nobody’s surprised.)

Back on-topic.

The program stack is only described abstractly by the Standards; as long as function calls nest properly, the C implementation is free to do as it pleases. The stack might be a linked list or voodoo. As such, there are basically no size limits per-frame or per-process in the Standards

Usually, of course, the stack is a chunk of contiguous memory, and with OS support new pages can be ~silently faulted in if you bump up against the stack bounds. This is only ever true for the first/“main” thread, and on UNIX its maximum size is described by the ulimit for the process; typically it’s 8..64 MiB. Secondary threads (started by/after main thread) do not have these luxuries, and are usually a fixed 8 or 16 MiB (in theory; you can use whatever size of stack your program can stand; typically stdio functions need like 64KiB of frame, for reference.

On top of that scheme there’s an alternate stack option where you tell the OS to use that stack if it takes a signal, in case the signal is caused by or causing a stack fault. If a signal handler returns via the normal path, the OS will restore the stack.

Embedded or μarch code may run with 8–64 KiB; usually the OS kernel has a limited stack as well.

So those are rough size totals/limits. But you don’t/can’t know for sure how much space remains in the stack, since that’s all up to the aft end of the compiler. There’s also (potentially) no knowing what funct

/r/C_Programming Thread