views:

315

answers:

6

Hi guys,

Is it possible to do a 'dump' on complex structures or even arrays in C++, in order to visually be able to check out what they've got inside them?

I'm thinking of something similar to print_r() or var_dump() in PHP.

Cheers, -Fin

+1  A: 

Not generally possible in C++ as it requires the use of reflection, which C++ does not have. You can of course write your own function to dump specific data structures.

anon
+7  A: 

The short answer is: no, unless you manually write such a routine yourself.

Which is often not a bad idea, but it has the usual problems of getting out of sync with the class if you are adding/changing members often. This is unavoidable because C++ lacks any form of introspection into structs.

If you decide to go that way, the best thing to do is to write an overload for std::ostream& operator<<(std::ostream& os, MyClass const&), which will enable your class to be printed to any IOStreams output stream.

j_random_hacker
anon
@Neil: Well, I disagree that it implies that, but I see your point. Implementing operator>>() is always harder, so I often don't bother, but it's always nice to have it too.
j_random_hacker
TYhe other issue is that it's not uncommon to want to have a formatted output method (op<<) AND a raw dump method, in which case the named approach can be more consistent.
anon
@Neil: Yes, you quite often want both. In those cases I tend to make op<<() the "raw, easy-to-parse" dump format that can be safely round-tripped with an op>>(), but it's totally a matter of preference.
j_random_hacker
+3  A: 

In addition to the other answers, depending on what you want it for and whether you care about portability, you can potentially look at getting the information you need from the debugging information that your compiler generates. You can parse the COFF/ELF/whatever format file from the build and this gives you the information you need to work out the names and types of the variables in your object.

James Sutherland
+2  A: 

If you add reflection to C++ (using a third-party library or vendor extensions), you can write a routine to use that reflection data to dump arbitrary structures. For example, I have some code that uses CERN's Reflex library to iterate over a class or structure's members and dump them to YAML.

Josh Kelley
+3  A: 

Usually debuggers are smart enough to do this.

In GDB you can use:

print structure

and in NTSD you can do the uber-cool:

dt -r structure

If you're just using this for debugging purposes, I highly recommend learning to use the debuggers. Even if its something you want to log as you (i.e. print a zillion times) you can set a breakpoint macro.

In NTSD:

bp yourdll!yourobject::yourfunction "dt -r structure;g"

And I'm sure there is a way to do it in GDB as well

jeffamaphone
A: 

Indeed, in an environment where you have gdb at your disposal and you compile your sources with debugging symbols enabled (e.g -ggdb) you can have your debugger (e.g. gdb from commandline or ddd when you want something graphically).

Consider this piece of code:

#include <string>
#include <vector>

struct test
{
    int a;
    float b;
    std::string c;
};

int main()
{
    std::vector<int> v;

    test t;
    t.a=1;
    t.b=2.0;
    t.c="hello there";


    return 0;
}

When asking gdb politely it can give me the following output:

(gdb) break 20
Breakpoint 1 at 0x8048622: file bla.cpp, line 20.
(gdb) run
Starting program: /home/edb/a.out 

Breakpoint 1, main () at bla.cpp:21
21      return 0;
(gdb) print t
$1 = {a = 1, b = 2, c = {static npos = 4294967295, 
    _M_dataplus = {> = {> = {}, }, _M_p = 0x96b6014 "hello there"}}}
(gdb) ping v
Undefined command: "ping".  Try "help".
(gdb) print v
$2 = { >> = {
    _M_impl = {> = {> = {}, }, _M_start = 0x0, _M_finish = 0x0, 
      _M_end_of_storage = 0x0}}, }

Edit: note that this data is available from a debugger context, for generating these dumps at runtime you will need to foresee your own dumping/formatting function, typically done by overloading the << operator in C++.

amo-ej1