[Issue 9326] New: writeln to simply show pointed data

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jan 16 02:25:25 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9326

           Summary: writeln to simply show pointed data
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2013-01-16 02:24:58 PST ---
A small demo program:


import std.stdio: writeln;
struct Foo {
    int x;
}
struct Node {
    Node* next;
}
void main() {
    Foo f = Foo(1);
    writeln("Simple struct: ", f);
    Foo* pf = new Foo(2);
    writeln("Struct from pointer: ", pf);
    Node* ptr = new Node(new Node(null));
    writeln("Singly linked list: ", ptr);
    Node* n1 = new Node(null);
    Node* n2 = new Node(n1);
    n1.next = n2;
    writeln("Circular singly linked list: ", n1);
}


Currently prints (dmd 2.062alpha):

Simple struct: Foo(1)
Struct from pointer: 1601F90
Singly linked list: 19C1F80
Circular singly linked list: 1601F60


For debugging, or just during the writing of code, I'd often like to see what's
pointed by those pointers.

So I suggest to enhance Phobos textual formatting a little, to produce
something simple like (on 64 bit pointers are longer):

Simple struct: Foo(1)
Struct from pointer: <1601F90*>Foo(2)
Singly linked list: <1601F80*>Node(<19C1F70*>Node(null))
Circular singly linked list: <1551F60*>Node(<1551F50*>Node(<1551F60*>...))


Notes:
- Numbers like 1551F50 are the pointer values in hex;
- The "*" helps remember that they are deferenced;
- The <> of <1551F50*> help better tell apart the pointer from the pointed
data.
- The ellipsis ... in Node(<1551F60*>...) means that this call to writeln has
already shown what this pointer points to.

- - - - - - - - - - - - - -

This change in textual formatting is not meant to influence the result of "%x",
just "%s":


import std.stdio: writefln;
struct Foo {
    int x;
}
void main() {
    Foo* pf = new Foo(100);
    writefln("%s", pf);
    writefln("%x", pf);
}


Currently prints:

1981F90
1981f90



After that improvement it should print something like:

<1581F90*>Foo(100)
1581f90

- - - - - - - - - - - - - -

To keep things simple it's better to not follow untyped pointers (void*):


import std.stdio: writefln;
struct Foo {
    int x;
}
void main() {
    Foo* pf = new Foo(100);
    void* ptr = cast(void*)pf;
    writefln("%s", pf);
}


It should print just:

1571F90

Or maybe something similar to:

<1581F90*>void

- - - - - - - - - - - - - -

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list