Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a stackalloc function for C?

Is there a stackalloc function implemented in C that allows you to allocate a variable length array on the stack, like stackalloc in C#?

like image 872
sashoalm Avatar asked Aug 28 '12 15:08

sashoalm


2 Answers

There's alloca but it's non-standard. Moreover, since C99 there's a feature called "Variable Length Arrays".

int n;
scanf("%d", &n);
int v[n]; /* Will fail badly if n is large. */

Used sparingly and with small values VLAs can be quite nice.

like image 173
cnicutar Avatar answered Oct 21 '22 10:10

cnicutar


alloca works a little like that, but you need to be very careful using it.

like image 5
Woodrow Douglass Avatar answered Oct 21 '22 12:10

Woodrow Douglass