gpt4 book ai didi

c++ - 编译器找不到 'aligned_alloc' 函数

转载 作者:搜寻专家 更新时间:2023-10-31 01:27:48 37 4
gpt4 key购买 nike

我正在尝试从 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 的工具链,但它没有。所以我想我不能接受提供的答案,直到我得到一个更具体的答案。

最佳答案

我没有使用 macOS,但我在使用自定义 g++ 的 linux 上遇到了类似的问题。如果您查看 cstdlib header ,就会发现类似

#if __cplusplus >= 201703L && defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
using ::aligned_alloc;
#endif

所以 aligned_alloc 仅在 C++17 可用且 glibcxx 支持时才被拉入 std 命名空间。如果定义了 _GLIBCXX_HAVE_ALIGNED_ALLOC,您可以检查 x86_64-linux-gnu/bits/c++config.h(或 macOS 上的类似内容)。如果不是,则您的 glibc 版本太旧。

对于 clang 和 libc++ 实现,如果定义了 _LIBCPP_HAS_C11_FEATURES,则 aligned_alloc 可用,这又取决于最新版本的 glibc。

作为替代方案,您可以使用 boost .

关于c++ - 编译器找不到 'aligned_alloc' 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52777209/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com