V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Akiyu
V2EX  ›  问与答

C++代码中的报错, 请指教一下

  •  
  •   Akiyu · 2019-07-27 09:21:53 +08:00 · 897 次点击
    这是一个创建于 1755 天前的主题,其中的信息可能已经有所发展或是发生改变。
    vector<string>& words;  
    sort(words.begin(), words.end(), [](const string &str1, const string &str2) {  
        return str1.size() > str2.size();  
    });  
    

    我的目的是将 words 中的 string 按照长度排序 (words 中大概有 1k 个 string)

    但是 lambda 函数中的比较如果是 >= 的话, 就会报错, heap buffer overflow

    AddressSanitizer: heap-buffer-overflow on address 0x63000000fe08 at pc 0x000000411eff bp 0x7ffcfab095c0 sp 0x7ffcfab095b8
    

    我的迷惑是 :

    1. 堆溢出? 怎么溢出的?

    2. 为什么 > 就可以?

    3 条回复    2019-07-27 09:30:29 +08:00
    AlohaV2
        1
    AlohaV2  
       2019-07-27 09:27:34 +08:00 via Android   ❤️ 1
    能否再贴完整一些的代码?
    richard1122
        2
    richard1122  
       2019-07-27 09:29:30 +08:00   ❤️ 3
    "Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines."

    因为返回 bool 要保证 comp(a, b) 和 comp(b, a) 是只有一个 true 的,在相等时候不可以返回 true
    richard1122
        3
    richard1122  
       2019-07-27 09:30:29 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   6230 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:45 · PVG 10:45 · LAX 19:45 · JFK 22:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.