Weird Strings??

This is a discussion on Weird Strings?? within the C++ Programming forums, part of the General Programming Boards category; String containing "cd" prints like this: Code: $3 = {static npos = 4294967295, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Weird Strings??

    String containing "cd" prints like this:

    Code:
    $3 = {static npos = 4294967295,
      _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data field   s>}, <No data fields>}, _M_p = 0x8b6004c "cd"}}
    Does Linux/GDB always print strings like this, or am I doing something wrong?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That looks right. A string variable is just an instance of the string class, and so the contents of that class will be output by the debugger. Some debugger allow you to customize how it is output, but I'm not familiar with how to do that in gdb.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    In any debugger, you could use something like this.
    Code:
    print str.c_str()
    That would give you the C string, which should print without any extra frills.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by dwks View Post
    In any debugger, you could use something like this.
    Code:
    print str.c_str()
    That would give you the C string, which should print without any extra frills.
    Ahh... Interesting...

    However, I'm running through GDB pointers A LOT to see what's going on with everything... Is there any way to do c_str() by default?
    Last edited by Paul22000; 07-10-2008 at 10:16 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Hmm. You could create functions like this
    Code:
    const char *c(std::string &str) {
        return str.c_str();
    }
    
    const char *c(const char *c) {
        return c;
    }
    and call them like "p c(whatever)". ("p" is short for "print".) It would work for C++ strings and char[] strings.

    Other than that, I can't think of any shorter way to do it at the moment.

    A few other things you might want to know: "$" evaluates to the previous result, $$ to the one before that. And you can use $N to access a specific result. (Notice that whenever you print something, it gets a number? Well, you can use $14 or $20 or whatever to get that exact value.)
    Last edited by dwks; 07-11-2008 at 02:00 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by dwks View Post
    Hmm. You could create functions like this
    Code:
    const char *c(std::string &str) {
        return str.c_str();
    }
    
    const char *c(const char *c) {
        return c;
    }
    and call them like "p c(whatever)". ("p" is short for "print".) It would work for C++ strings and char[] strings.

    Other than that, I can't think of any shorter way to do it at the moment.

    A few other things you might want to know: "$" evaluates to the previous result, $$ to the one before that. And you can use $N to access a specific result. (Notice that whenever you print something, it gets a number? Well, you can use $14 or $20 or whatever to get that exact value.)
    Damn... Smart... Haha

    Why the "const" though?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Because it says it cannot be modified, so the compiler will complain if you try.
    The .c_str() function returns a const char* pointer, so it is wrong to modify it in the first place, and as C++ is type strict, it won't compile unless you return a const char*.
    [And if you modify it, it will result in undefined behavior.]
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    In any debugger, you could use something like this.
    Code:
    print str.c_str()
    But why would you? The entire string can be read from the debugger, it just gives some extra information about its trait and allocator classes.
    Writing extra code for debugging might introduce new errors (have you never written erroneous test-code, which made you spending hours to search a bug which wasn't there? I have..).

    Simply ignore the extra information from the debugger and everything is just fine...

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    But if you're examining a lot of strings, it might be slower to do as you suggest. Your eye doesn't easily skip over the other stuff GDB prints, especially if the string is only a few characters long.

    Yes, introducing new test code can introduce new bugs. But as long as you're aware of that fact, such code can really make your life easier. For example, one time I was debugging a tree, where nodes consisted of pointers; so in order to examine the whole tree, I had to use a lot of print statements. I wrote a simple function which dumped the whole tree, and it made my life a lot easier . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by dwks View Post
    But if you're examining a lot of strings, it might be slower to do as you suggest. Your eye doesn't easily skip over the other stuff GDB prints, especially if the string is only a few characters long.

    Yes, introducing new test code can introduce new bugs. But as long as you're aware of that fact, such code can really make your life easier. For example, one time I was debugging a tree, where nodes consisted of pointers; so in order to examine the whole tree, I had to use a lot of print statements. I wrote a simple function which dumped the whole tree, and it made my life a lot easier . . . .
    Haha! That's pretty much exactly what I have to do!

    For the first part of the project, we did a linked list representation, so I just printed every linked list in the trie out to screen.

    For the second part, now we have to do trees.

    Kind of off topic, but what's a good way to print a tree to command prompt?? Did you actually do spaces between the nodes, or just say like "left child: value" etc?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by Paul22000 View Post
    Haha! That's pretty much exactly what I have to do!

    For the first part of the project, we did a linked list representation, so I just printed every linked list in the trie out to screen.

    For the second part, now we have to do trees.

    Kind of off topic, but what's a good way to print a tree to command prompt?? Did you actually do spaces between the nodes, or just say like "left child: value" etc?
    I indented the nodes with spaces to get a graphical representation. Something like this. (Which I just typed up in this box and may not work properly.)
    Code:
    struct data {
        int data;
        struct data *next;
        size_t nexts;
    };
    
    void dump_node(struct data *tree, int indent);
    void print_indent(int indent);
    
    void dump_data(struct data *tree) {
        dump_node(tree, 0);
    }
    
    void dump_node(struct data *tree, int indent) {
        size_t x;
        
        print_indent(indent);
        std::cout << tree->data << std::endl;
        
        for(x = 0; x < tree->nexts; x ++) {
            dump_node(tree, indent + 1);
        }
    }
    
    void print_indent(int indent) {
        int x;
        for(x = 0; x < indent; x ++) std::cout << "    ";
    }
    That's for a sort of trie-like structure, not a binary tree (which is what you have, right?). It might generate printouts like this.
    Code:
    0
        1
        2
            4
            6
        3
    You can print a tree however you like . . . I like to use whitespace, however. Your eye easily passes over it, and you still get an idea of what the structure looks like.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Ohhhh ok, so don't put spaces on the left. That makes it easier!

    I'm confused as to how you have it, so 1/2 are children of 0.

    4 and 6 are children of 2?

    And 3 is child of 6?

    For this project the data is stored in a trie AND a secondary data structure (first linked lists, and now on to binomial heaps). Basically to make various operations like merge faster, since we have the choice of accessing the data with either of the two data structures.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You can print the data any way you want. The way I printed it is suitable for the type of structure I had:
    Code:
    struct data {
        int data;
        struct data *next;
        size_t nexts;
    };
    Basically, every structure pointed to zero or more (many more) other structures.

    I think with complex structures like a binomial heap, unless you can come up with a way to depict them graphically, you might have to print them linearly.

    You could try to record everything you're going to print and then format it before you print it -- which would let you display a binary tree, for example -- but that's probably just overkill for a debugging function.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-15-2008, 10:24 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. weird strings
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-06-2002, 01:42 PM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21