Alloca implementation

前端 未结 11 1002
误落风尘
误落风尘 2020-11-29 08:50

How does one implement alloca() using inline x86 assembler in languages like D, C, and C++? I want to create a slightly modified version of it, but first I need to know how

11条回答
  •  生来不讨喜
    2020-11-29 09:51

    If you can't use c99's Variable Length Arrays, you can use a compound literal cast to a void pointer.

    #define ALLOCA(sz) ((void*)((char[sz]){0}))
    

    This also works for -ansi (as a gcc extension) and even when it is a function argument;

    some_func(&useful_return, ALLOCA(sizeof(struct useless_return)));
    

    The downside is that when compiled as c++, g++>4.6 will give you an error: taking address of temporary array ... clang and icc don't complain though

提交回复
热议问题