c++ - 如何在 Linux 上强制执行 vector 下标超出范围调试断言

标签 c++ linux visual-c++

此代码适用于 linux。

#include <vector>
#include <iostream>

using namespace std;


int main () 
{
    vector<int> v(10, 0);

    cout << v[100];

    return 0;
}

operator[] 用注释声明

      // element access
      /**
       *  @brief  Subscript access to the data contained in the %vector.
       *  @param __n The index of the element for which data should be
       *  accessed.
       *  @return  Read/write reference to data.
       *
       *  This operator allows for easy, array-style, data access.
       *  Note that data access with this operator is unchecked and
       *  out_of_range lookups are not defined. (For checked lookups
       *  see at().)
       */
      reference
      operator[](size_type __n) _GLIBCXX_NOEXCEPT
      { return *(this->_M_impl._M_start + __n); }

但是 MSVS 编译器会警告此类下标超出范围的情况。有什么方法可以模仿它的行为吗?

最佳答案

其实,我的一个同事已经找到了想要的答案:

-D_GLIBCXX_DEBUG

标志激活 libstdc++ 的 Debug模式。

关于c++ - 如何在 Linux 上强制执行 vector 下标超出范围调试断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45104340/

相关文章:

c# - 从 native 可视化 C++ 代码调用 C# .dll

c++ - if 语句正在检查什么条件

c++ - 如何在不同的线程上调用 glReadPixels?

c++ - 这是使用 alloca 的好理由吗?

c++ - 你如何禁用 CBitmapButton?

visual-c++ - MFC ComboBox 在删除其项目后使列表保持无效

c++ - C和C++中静态变量初始化的区别

linux - 使用脚本提取文本

linux - 以 root 用户身份运行 ElasticSearch

Linux CSV 根据大文件中的旧日期删除重复项(超过 100k 记录)