【问题标题】:std::aligned_alloc() missing from visual studio 2019?Visual Studio 2019 中缺少 std::aligned_alloc()?
【发布时间】:2020-11-07 18:48:07
【问题描述】:

尽管我确实尝试分别启用 std:c++latest 和 c++17,但我无法使用 c++17 中添加的 std::aligned_alloc() 函数。这是真实生活吗? Visual Studio 2019 是否完全没有在标准中实现像这样的古老功能(也是一个非常重要的功能)?其他人可以确认吗?

我指的功能:

https://en.cppreference.com/w/cpp/memory/c/aligned_alloc

【问题讨论】:

    标签: visual-studio c++17


    【解决方案1】:

    来自Microsoft C++ language conformance table for VC++ 2019:

    C11 - 通用 CRT 实现了 C++17 所需的 C11 标准库的部分,除了 C99 strftime() E/O 替代转换说明符,C11 fopen() 独占模式, 和 C11 aligned_alloc()。后者不太可能实现,因为 C11 以与 Microsoft 的 free() 实现不兼容的方式指定了aligned_alloc():即 free() 必须能够处理高度一致的分配。

    VC++ 有编译器特定的_aligned_malloc 用于对齐到 2 次幂值的内存,另请参阅 What can I use instead of std::aligned_alloc in MS Visual Studio 2013?

    【讨论】:

    • 难道他们只能将 c++17 alligned_alloc() 绑定到 _alligned_malloc() 吗?为什么他们不符合标准?到目前为止,Visual Studio C++ 基本上是另一种编程语言。我真的需要制作一个自定义的 alligned_alloc() 函数,在使用 Visual Studio 时绑定到 _alligned_malloc() 并在使用另一个编译器时绑定到其他东西(即 std::aligned_alloc 但现在可能没有人符合标准)?跨度>
    • @MatiasChara std 要求aligned_alloc 返回的内存块与常规reallocfree“兼容”。 MSVC 的 _aligned_malloc 需要 _aligned_realloc_aligned_free
    • 猜猜如果我想让我的代码可移植,我应该使用宏来交替使用 std::aligned_alloc() 和 _aligned_malloc 以及它们各自的免费和 realloc 版本(当然只是用绑定到这些的宏)。正确的? _aligned_malloc 和家族是否只能在 windows 中使用?
    • 这些是 UCRT 的一部分,仅表示 Windows。但是,问题比仅仅添加一些宏包装器更深。在std 中,您可以编写函数char *double_string(char *str) { return realloc(str, 2 * strlen(str) + 1); } 并调用相同的函数,无论str 是从malloc 还是aligned_alloc 获得的。使用 MSVC,您需要单独的函数。
    猜你喜欢
    • 2019-09-03
    • 2021-08-26
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2019-11-30
    • 2019-09-16
    • 2021-03-11
    • 2020-09-14
    相关资源
    最近更新 更多