之前我在Visual Studio中使用过调试模式,但我从来没有使用内存窗口。如果我有一个简单的应用程序来计算a = b + c并使b = 8且c = -2,那么如何在不使用监视的情况下在内存窗口中找到地址a,b和c及其值?
当我尝试时,我看到了大量的“胡言乱语”,我无法理解。这是一个截图:
如果我想做同样的事情,但在Linux环境中,我怎么能实现这个目标呢?
答案 0 :(得分:13)
在Visual Studio中查找变量地址的一种方法是使用QuickWatch窗口(如果您不知道热键,则在调试菜单下, Ctrl + Alt < / kbd> + Q )。如果您输入&a
,则会显示变量a
的地址。然后,您可以在内存窗口中输入该地址。或者您可以在内存窗口中输入&a
。
但要查看内存窗口中的所有变量,它们需要在彼此的几个字节内,因为它显示连续的内存。对于堆栈上的局部变量,这通常不是问题。对于整数变量,通过右键单击存储器窗口并更改布局,可以更容易地以可读格式查看它们(例如,选择带有符号显示的4字节整数)。
说了这么多,看起来使用监视窗口要简单得多,因为所有内容都已经很好地标记了,并且很容易判断哪个值与哪个变量相关联。
答案 1 :(得分:3)
I saw tons of "gibberish": a small example may help (especially for the next readers :)
Copy/paste the following code and debug it:
class File {
public $file = '';
public $data = '';
public function __construct($file = '') {
$this->file = $file;
}
function put($create = false) {
// Check if file is writeable and put content, won't create new file unsless $create is set to true
if($create == false) {
is_writable($this->file) ? file_put_contents($this->file, $this->data, LOCK_EX) : exit;
} else {
file_put_contents($this->file, $this->data);
}
}
}
Run the Visual Studio debugger, add watch to all variables (right click each variable and click "Add Watch"). Now if it is not open, open the watch window (menu Debug → Window → *Watch), and drag/drop the variable $f = new File();
$f->file = "/www/htdocs/somehost/folder/data/helloworld.txt";
$f->data = "Hello world";
$f->put('true');
from the watch window to the memory window. You'll obtain the following:
You'll observe that the value .site-bar .nav {
float: left;
width: 100%;
padding-top: 3px;
text-align: center;
}
.nav ul {
margin: 0;
padding: 0;
list-style: none;
display: inline-block;
}
appears at this address. In hexadecimal, 0x41 is equal to 65. So you see that the address of variable struct MyStruct
{
int age;
char code_1;
char code_2;
char code_3;
};
int main()
{
int int_variable = 65;
int* adresse_int_variable = &int_variable;
int int_variable2 = 10000;
char char_variable_1 = 'A';
char char_variable_2 = 'B';
cout << " sizeof(int_variable) " << sizeof(int_variable) << endl;
cout << " sizeof(char_variable_1) " << sizeof(char_variable_1) << endl;
MyStruct mystruct;
mystruct.age = int_variable2;
mystruct.code_1 = 'A';
mystruct.code_2 = char_variable_2;
mystruct.code_3 = int_variable;
return 0;
}
effectively contains 65. (Note that in reality the memory contains bits: 01000001, but it is represented in hexadecimal to ease of the reading.)
Enter adresse_int_variable
in the memory window, you'll get:
41
holds the value int_variable
, and in hexadecimal it is &int_variable2
. Now look for values stored for variables int_variable2
and 10000
: you see 0x2710
and char_variable_1
. This is the way char_variable_2
and 0x41
are encoded in the ASCII Table. Note that in memory 0x42
and A
are the same.
Finally enter B
in the memory window, and you'll see:
This corresponds to the memory of the int_variable
variable, that holds four variables (an char_variable_1
and three &mystruct
s). You see the mystruct
variable (int
) and the three following characters: char
, age
and 10000 = 0x2710
that are stored as A
, B
, 65
(from right to left). Note that in the right part of the window you can see 0x41
as string representation of the memory (if not right click the window and click ANSI).
Try with more complex variables, read about endianness and data structure alignment. See also the memory window page on MSDN.
答案 2 :(得分:0)
尝试使用char *并在Memory窗口中打开其地址。解释它的输出会更简单。至于Linux环境,我猜你可以在GDB上使用这个教程:http://www.ofb.net/gnu/gdb/gdb_56.html
不时看到something like this非常有趣:)
答案 3 :(得分:0)
我必须查看反汇编代码并找出寄存器的值。