Thread: What does c_str() do?

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    What does c_str() do?

    Hi, was just wondering what does c_str() really do..I use it a lot when I am forced to pass pointers to a string in allegro...but no idea what it does.

    Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.

    A terminating null character is automatically appended.
    does that mean it copies the string from the string object, adds the terminating null character in a separate memory address?

    When using the printf(..) it appears they have different addresses..so in that case wouldn't it better to use the char* from the very beginning?

    Also one thing I noticed is when I did this
    Code:
     
         printf("%p %p",  d, s) ; 
         printf("%p %p",  s, d) ;
    The first printf would print the addresses..the second wouldn't..if i used %s on both to see the content it, crashes...i don't know why it should..printf() is still part of C++..?

    Thanks
    You ended that sentence with a preposition...Bastard!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Yes, it *may* copy the entire string into a different memory location, add a 0-byte there, and return the address. Usually, though, the string itself will be stored in the memory as a continuous array of bytes, resizing/copying when required. In that case, an implementation will likely simply add a 0 byte to the end of the array and return the internal pointer of the character array. So usually it won't copy anything.

    I'm not sure what those d and s are in printf. I suspect one of them is not a pointer. Maybe it's of type std::string? That's simply of type class, so printing it as a pointer makes no sense at all (unless you take the address of the std::string of course). And printing an std::string using the %s format string crashes of course, as std::string is not a valid pointer to a 0-terminated string. That's what you've got c_str for.

    You seem to be mixing up a lot here. Do you know what classes are? Because an std::string is simply a class, and it is usually simply a wrapper for a character array so that it can resize it if needed.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    oh yeah sorry..
    s is an object of the string class.. i was trying to print the address &s
    that was a typo on my part. Should it not be able to print the address of an object, like an address of a struct object in C?

    so if s.c_str() simply adds a newline.would the string object forever have a null character at its end..assuming there were no modification of the string onwards
    You ended that sentence with a preposition...Bastard!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    &s is the address of the string object, which does not have to match (and probably doesn't) the address of the char data inside the object.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    oh there was a i missed the & that was why it crashed..apart from that it works..
    &s is the address of the string object, which does not have to match (and probably doesn't) the address of the char data inside the object.
    you are right..
    &s and &s[0] prints different addresses....why?
    You ended that sentence with a preposition...Bastard!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    &s and &s[0] prints different addresses....why?
    Consider this String class, shown only in part:
    Code:
    class String
    {
    public:
        char& operator[](std::size_t index)
        {
            return data[index];
        }
    
        // ...
    private:
        char* data;
        // ...
    };
    Now, consider this code snippet:
    Code:
    String s = "hello world!";
    std::cout << &s << ' ' << &s[0] << std::endl;
    Would you expect that &s and &s[0] would result in the same thing being printed?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I am not sure what that size_t means..and googling it says it represents amount of memory an object requires..does that mean it is like sizeof(data_t)?

    but yeah..I still think it should point to the same address..

    data variable is a pointer I assume to the first character of that object..and uses the index in subscript notation as a pointer offset (I hope I am right in thinking that)
    so s[0] should still print the same as &s..

    EDIT:
    is there a term for this?
    Last edited by Eman; 01-18-2011 at 08:40 AM.
    You ended that sentence with a preposition...Bastard!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    I am not sure what that size_t means..and googling it says it represents amount of memory an object requires..does that mean it is like sizeof(data_t)?
    std::size_t is an unsigned integer type that is the type of the result of sizeof.

    Quote Originally Posted by Eman
    but yeah..I still think it should point to the same address..

    data variable is a pointer I assume to the first character of that object..and uses the index in subscript notation as a pointer offset (I hope I am right in thinking that)
    so s[0] should still print the same as &s..
    Oh yes, I overlooked that. However, as I am the maintainer of this String class, I now show you what I left out of my implementation. Not all of it of course (it's a trade secret), but just a little bit more:
    Code:
    class String
    {
    public:
        char& operator[](std::size_t index)
        {
            return data[index];
        }
    
        // ...
    private:
        size_t size;
        char* data;
        size_t capacity;
    };
    Do you still think that "&s[0] should still print the same as &s"?

    EDIT:
    Actually, the above is rubbish, but I shall leave it as it is a good thing to think about, namely, that the exact implementation is... implementation defined.

    Here's why it is rubbish: originally, I hoped that you would see that the location in memory of what data points to is not the location of data. I did not expect you to respond as you did and managed to get confused too... but it shows also that you are confused between an array and a pointer.
    Last edited by laserlight; 01-18-2011 at 08:35 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    em, not to look and sound gullible...but I don't see a difference except 2 more variables..
    data still uses index as an offset?
    and you wrote your own string class? wow cool
    You ended that sentence with a preposition...Bastard!

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    em, not to look and sound gullible...but I don't see a difference except 2 more variables..
    data still uses index as an offset?
    Why do you think that &s and &s[0] should have the same address? Why does the fact that data using index as an offset has any bearing on the address of the object and the location of what data points to?

    Quote Originally Posted by Eman
    you wrote your own string class?
    No, I did not, though I have written container classes for practice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Eman View Post
    em, not to look and sound gullible...but I don't see a difference except 2 more variables..
    data still uses index as an offset?
    and you wrote your own string class? wow cool
    Let's ignore classes for a moment, because the same thing would happen in a struct:
    Code:
    struct string_object {
        int size;
        char* data;
        int capacity;
    };
    
    string_object foo;
    would you expect foo and foo.data to be at the same location in memory?

  12. #12
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by tabstop View Post
    Let's ignore classes for a moment, because the same thing would happen in a struct:
    Code:
    struct string_object {
        int size;
        char* data;
        int capacity;
    };
    
    string_object foo;
    would you expect foo and foo.data to be at the same location in memory?
    no they have different addresses...but are in the same memory location..or segment

    I am not sure as to how to explain it
    but foo would start say at 0x0000
    and foo.data at 0x0008
    You ended that sentence with a preposition...Bastard!

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Eman View Post
    no they have different addresses...but are in the same memory location..or segment

    I am not sure as to how to explain it
    but foo would start say at 0x0000
    and foo.data at 0x0008
    So if I have a class that looks exactly the same..................

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tabstop
    would you expect foo and foo.data to be at the same location in memory?
    Next question: would you expect foo and foo.data[0] to be at the same location in memory?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by tabstop View Post
    So if I have a class that looks exactly the same..................
    I don't get what you mean?


    would you expect foo and foo.data to be at the same location in memory?
    oh no..foo and foo.data[0] is not the same thing..
    hold on what're you saying (baffled), that the characters are like member variables of the string object?
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed