aligned_alloc visibilty

user1234 user1234 at 12.de
Sat Apr 12 15:32:05 UTC 2025


On Saturday, 12 April 2025 at 03:22:06 UTC, faceless wrote:
> What is the purpose of hiding the `aligned_alloc` declaration 
> like this in `core.stdc.stdlib`?
> ```d
>  29    version (CRuntime_Glibc)
>  30        version = AlignedAllocSupported;
>  31    else version (CRuntime_Newlib)
>  32        version = AlignedAllocSupported;
>  33    else {}
> ...
> 184    /// since C11
> 185    version (AlignedAllocSupported)
> 186    {
> 187        void* aligned_alloc(size_t alignment, size_t size);
> 188    }
> ```
> —— my system is definitely not `CRuntime_Glibc` or 
> `CRuntime_Newlib` as shown by the output of the following 
> example:
> ```d
> import core.stdc.stdlib : malloc, free, aligned_alloc;
>
> // extern(C) void* aligned_alloc(size_t alignment, size_t 
> size); // uncomment to compile
>
> import core.stdc.stdio:printf;
> void main()
> {
>     int* p1;
>     p1 = cast(int*)malloc(10* p1.sizeof);
>     printf("default-aligned addr:   %p\n", cast(void*)p1);
>     free(p1);
>
>     int* p2;
>     p2 = cast(int*)aligned_alloc(1024, 1024* p2.sizeof);
>     printf("1024-byte aligned addr: %p\n", cast(void*)p2);
>     free(p2);
> }
> ```
>
> —— output:
> ```
> aligned.d(1): Error: module `core.stdc.stdlib` import 
> `aligned_alloc` not found
> import core.stdc.stdlib : aligned_alloc;
>        ^
> ```
>
> —— but it compiles fine when compiled with 
> `-version=AlignedAllocSupported` or if I declare the 
> `aligned_alloc` myself. Here is the output of the above example 
> with the `-version` flag set on my system:
> ```
> default-aligned addr:   0x600002d08050
> 1024-byte aligned addr: 0x7fafb380e800
> ```

Maybe there's a `version (CRuntime_XXX)` missing for the C lib 
you are using ? If so that looks like a druntime bug. But as 
there's not that much libc implementation what is the one you are 
on, Musl maybe ?

runtime library issues go here :  
https://github.com/dlang/dmd/issues


More information about the Digitalmars-d-learn mailing list