Issue 19159 - `alloca` does not work in -betterC
Summary: `alloca` does not work in -betterC
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: betterC
Depends on:
Blocks:
 
Reported: 2018-08-12 03:55 UTC by Mike Franklin
Modified: 2022-12-17 10:37 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mike Franklin 2018-08-12 03:55:13 UTC
extern(C) void main()
{
    import core.stdc.stdlib : alloca;
    
    enum length = 20;
    auto msg = (cast(char*)alloca(length))[0 .. length];
}

https://run.dlang.io/is/dHAqPo

$ dmd -betterC main.d
error: undefined reference to '__alloca'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1

Works fine with LDC, however: https://run.dlang.io/is/m45jG1

Also, `alloca` should not be in `core.stdc.stdlib`; it's not a standard C function.

It may also be better to allocate static arrays on the stack when their length is supplied at runtime.  See https://issues.dlang.org/show_bug.cgi?id=18788.  That would make `alloca` and `__alloca` just an implementation detail that users wouldn't even need to use directly.
Comment 1 Walter Bright 2018-08-14 20:13:51 UTC
alloca() works by calling a compiler-specific function to implement it. This means it has to be customized for each supported C compiler. Currently for dmd, this has only been done for dmc (Win32).

Whether it's standard C or not, functions that are in the corresponding compiler's stdlib.h should be in core.stdc.stdlib.