【发布时间】:2019-03-17 13:09:34
【问题描述】:
我正在尝试从aligned alloc 启动示例代码:
#include <cstdio>
#include <cstdlib>
int main()
{
int* p1 = static_cast<int*>(std::malloc(10*sizeof *p1));
std::printf("default-aligned address: %p\n", static_cast<void*>(p1));
std::free(p1);
int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
std::printf("1024-byte aligned address: %p\n", static_cast<void*>(p2));
std::free(p2);
}
我的编译器给了我这个错误:
$ g++-mp-8 main.cpp -std=c++17
main.cpp:10:38: error: no member named 'aligned_alloc' in namespace 'std'
int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
我正在使用 macOS High Sierra 10.13.6 并尝试使用 Macport 的 GCC 7.3.0、8.2.0 和 CLang(Apple LLVM 版本 10.0.0)编译此代码,它们都会产生相同的错误。
编辑:无论是否存在std::,它都不起作用。
Edit2:我安装了 macOS Mojave 并没有解决问题。我希望它会重新安装 macOS 的工具链,但它没有。所以我想在得到更具体的答案之前,我无法接受提供的答案。
【问题讨论】:
-
您链接的示例代码在
std中没有aligned_alloc。去掉aligned_alloc前面的std::还能用吗? -
@Blaze 不,它不起作用。
-
@Blaze 通过包含
cstdlib,Verloren 获得了来自stdlib.h的C 库功能,并将其放置在命名空间std中。事实上,单击参考页面底部指向 C++ 等效项的链接显示了与问题中的示例相同的示例。 -
这几乎可以肯定只是您的工具链中缺少的功能。你能链接到它的文档/状态吗?例如High Sierra 上的 XCode 9 也没有
std::optional和其他一些东西,尽管名义上与 C++17 兼容。