Why is the use of alloca() not considered good practice?

前端 未结 22 2457
甜味超标
甜味超标 2020-11-22 03:00

alloca() allocates memory on the stack rather than on the heap, as in the case of malloc(). So, when I return from the routine the memory is freed.

22条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 03:34

    Old question but nobody mentioned that it should be replaced by variable length arrays.

    char arr[size];
    

    instead of

    char *arr=alloca(size);
    

    It's in the standard C99 and existed as compiler extension in many compilers.

提交回复
热议问题