clang 标准库错误或 c++未定义的行为?

发布于 2025-01-13 09:08:12 字数 1705 浏览 4 评论 0原文

以下 C++ 程序是否包含任何未定义行为

int
main()
{
        struct entry
        {
                uint32_t hash;
                uint32_t idx;
        };
        entry arr[31] = {
            {   7978558,  0}, {   9241630,  1}, {  65706826,  2},
            { 639636154,  3}, {1033996244,  4}, {1225598536,  5},
            {1231940272,  6}, {1252372402,  7}, {2019146042,  8},
            {1520971906,  9}, {1532931792, 10}, {1818609302, 11},
            {1971583702, 12}, {2116478830, 13}, { 883396844, 14},
            {1942092984, 15}, {1274626222, 16}, { 333950222, 17},
            {1265547464, 18}, { 965867746, 19}, {1471376532, 20},
            { 398997278, 21}, {1414926784, 22}, {1831587680, 23},
            { 813761492, 24}, { 138146428, 25}, { 337412092, 26},
            { 329155246, 27}, {  21320082, 28}, {1751867558, 29},
            {1155173784, 30},
        };

        std::sort(std::begin(arr), std::end(arr),
                  [](entry a, entry b) { return a.hash <= b.hash; });
}

当我使用 gnu c++ 编译器或 12.0.0 之后的任何 clang/llvm 编译时,程序运行正常。但是,当我使用 clang 版本 12.0.0(我的 Mac 笔记本电脑上附带的默认编译器)编译它时,它在 std::sort() 内部崩溃,如下所示:

$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ g++ -g -std=c++11 bug.cc
$ ./a.out
Segmentation fault: 11

另外,如果我将其更改为使用std::vector 而不是固定大小的数组。使用 clang 12.0.0 编译时, std::sort() 将永远不会返回

Does the following C++ program contain any undefined behavior?

int
main()
{
        struct entry
        {
                uint32_t hash;
                uint32_t idx;
        };
        entry arr[31] = {
            {   7978558,  0}, {   9241630,  1}, {  65706826,  2},
            { 639636154,  3}, {1033996244,  4}, {1225598536,  5},
            {1231940272,  6}, {1252372402,  7}, {2019146042,  8},
            {1520971906,  9}, {1532931792, 10}, {1818609302, 11},
            {1971583702, 12}, {2116478830, 13}, { 883396844, 14},
            {1942092984, 15}, {1274626222, 16}, { 333950222, 17},
            {1265547464, 18}, { 965867746, 19}, {1471376532, 20},
            { 398997278, 21}, {1414926784, 22}, {1831587680, 23},
            { 813761492, 24}, { 138146428, 25}, { 337412092, 26},
            { 329155246, 27}, {  21320082, 28}, {1751867558, 29},
            {1155173784, 30},
        };

        std::sort(std::begin(arr), std::end(arr),
                  [](entry a, entry b) { return a.hash <= b.hash; });
}

When I compile with gnu c++ compiler or any clang/llvm after 12.0.0, the program works fine. However, when I compiled it with clang version 12.0.0 (the default compiler shipped on my Mac laptop), it crashed inside of std::sort() as following:

$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ g++ -g -std=c++11 bug.cc
$ ./a.out
Segmentation fault: 11

Also if I change it to use std::vector instead of fixed size array. The std::sort() will never return when compiled with clang 12.0.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2025-01-20 09:08:12

是的,比较器不是严格的弱排序,它违反了 std::sort 的前提条件,导致未定义的行为。

对于两个参数 ab(可能相同),严格的弱排序 comp 永远不应该同时计算 comp(a,b)comp(b,a)true。换句话说,它应该模拟内置 < 的行为,而不是 <=

因此,在您的代码中,它应该是 <,而不是 <=,以使其成为严格的弱排序。

Yes, the comparator is not a strict weak ordering which violates the preconditions of std::sort, resulting in undefined behavior.

For two arguments a and b (possibly identical), a strict weak ordering comp should never evaluate both comp(a,b) and comp(b,a) to true. In other words, it should model the behavior of the built-in <, not <=.

So in your code it should be <, not <=, to make it a strict weak ordering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文