c++ - 我使用什么 C++ 编译器选项来触发数组越界错误?

标签 c++ arrays

下面的代码中存在数组越界问题和缺失的 delete []。

使用以下选项编译代码不会触发任何错误:

g++ -std=c++2a -Wall -pedantic -fstack-protector-all test.cpp

#include <iostream>

class Base {
public:
    Base() { std::cout << "Base()\n";  }
    ~Base() { std::cout << "~Base()\n"; }
   int m_counter;
};


class Derived: public Base {
public:
    Derived() { std::cout << "Derived()\n"; }
    ~Derived() { std::cout << "~Derived()\n"; }
};


int main() {
    Base* b = new Derived[10];
    std::cout << b[10].m_counter << '\n';
    delete b;
    return 0;
}

关于我在这里可能遗漏了什么的任何指示?

最佳答案

我认为您正在寻找 -fsanitize=bounds

关于c++ - 我使用什么 C++ 编译器选项来触发数组越界错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58052273/

相关文章:

java - 将数组转换为不带括号、逗号或空格的字符串

c - 这个指针应该这样操作吗?基础C

c++ - 如何使 live555 服务器使用 rtsp 地址而不是文件进行流式传输

c++ - 通过引用将对象传递给C++ 11中的std::thread

c++ - for 循环的首选评论风格是什么? (C++,但也应该与其他语言相关)

C - fscanf 进入动态数组崩溃

arrays - 如何在 Rust 中创建并传递以空字符结尾的 C 字符串数组 (char**)?

c++ - MISRA C++ 2008 奇怪警告

c++ - 如何优化这个 find_if 代码?

arrays - 比较两个数组哈希