在不知道类型的情况下打印数据? [英] Printing data without knowing the type?

查看:74
本文介绍了在不知道类型的情况下打印数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打印一些结构以用于调试目的.由于代码库很大,我很难找到需要查找的确切成员.是否有某种方法可以打印出整个结构?还是至少知道它是什么类型的结构,以便我可以回过头来研究它的定义?

I need to print some structure for debugging purposes.Since the code base is huge i am having difficulty locating the exact member I need to look into.Is there some way I could print out the entire structure? or atleast know what type of structure it is so that I can go back and look into it's definition?

推荐答案

使用调试器.大多数调试器会在您遇到断点时为您提供查看整个结构内容的选项.在* nix上,最受欢迎的调试器是 gdb ,而在Windows上则是最受欢迎的IDE(包括调试器)是 Visual Studio .我链接的两个站点都有啤酒免费下载链接.

Use a debugger. Most debuggers give you an option to see the contents of an entire structure whenever you hit a breakpoint. On *nix the most popular debugger is gdb, and on Windows the most popular IDE (which includes a debugger) is Visual Studio. Both the sited I linked to have free-as-in-beer download links.

在gdb中,可以使用break命令设置一个断点,一旦到达断点,就可以使用print命令打印结构的内容.更具体地说,您可以编译包含的调试数据(gcc中的-g标志),然后使用

In gdb, you can set a breakpoint with the break command, and once you reach the breakpoint you can print the contents of the struct with the print command. More specifically, you can compile with debugging data included (the -g flag in gcc), and then use

$ gdb debugging_executable
Some basic information about GDB gets printed here
> break main.c:100
> run
> print struct_variable

也值得研究一下步骤并继续执行命令.

It's also worth looking into the step and continue commands.

在Visual Studio中,您可以通过双击源代码行的左侧(编辑器左侧有一个灰色条)来设置断点,然后将鼠标悬停在变量名称上以检查内容一次您到达了断点.

In Visual Studio, you can set a breakpoint by double-clicking just to the left of the source line (there is a gray bar on the left side of the editor), and mouse over the variable name to inspect the contents once you reach the breakpoint.

这篇关于在不知道类型的情况下打印数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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