简单的代码导致错误读取变量:无法访问地址处的内存 [英] Simple code cause error reading variable: Cannot access memory at address

查看:5403
本文介绍了简单的代码导致错误读取变量:无法访问地址处的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用启用了python的gdb MinGW-builds 。我遇到一个错误。这是一个相当简单的代码,它在MSVC下调试时工作正常。

I am trying to use a python-enabled gdb MinGW-builds. And I run into an error. This is a rather simple code, and It works fine when debugging under MSVC.

D:\CppProject\c1\bin\Debug>gdb c1.exe
GNU gdb (GDB) 7.6
(copyright, license, bug report, etc omitted here)
Reading symbols from D:\CppProject\c1\bin\Debug\c1.exe...done.
(gdb) l
1       #include <iostream>
2       #include <vector>
3
4       using namespace std;
5
6       int main()
7       {
8           vector<string> v;
9           v.push_back("first");
10          v.push_back("second");
(gdb)
11          cout<<v[0]<<endl;
12          cout<<v[1]<<endl;
13
14          return 0;
15      }
(gdb) break 11
Breakpoint 1 at 0x4016c9: file D:\CppProject\c1\main.cpp, line 11.
(gdb) run
Starting program: D:\CppProject\c1\bin\Debug\c1.exe
[New Thread 1256.0xbe8]

Breakpoint 1, main () at D:\CppProject\c1\main.cpp:11
11          cout<<v[0]<<endl;
(gdb) p v
$1 = std::vector of length 2, capacity 2 = {"first", "second"}
(gdb) p v[0]
$2 = <error reading variable: Cannot access memory at address 0x29a2ca50>


推荐答案

?我怀疑编译器优化可能会导致您的麻烦。

This may sound basic - did you compile with -O0 ? I suspect that compiler optimization might cause your trouble.

这篇关于简单的代码导致错误读取变量:无法访问地址处的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆