Results 1 to 19 of 19

Thread: QString use .c_str()?

  1. #1
    Join Date
    Aug 2010
    Posts
    1
    Platforms
    Unix/X11

    Default QString use .c_str()?

    Qt 4.5 QString have a way to output a null terminaded string ( QString.cstr() )

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Could also do this:

    Qt Code:
    1. #include <string>
    2.  
    3. char * c_string = QString().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Quote Originally Posted by jonthom View Post
    Could also do this:

    Qt Code:
    1. #include <string>
    2.  
    3. char * c_string = QString().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 
    edit*

    Qt Code:
    1. const char * c_string = QString().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    This code is incorrect. It will segfault in many situations.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    how so? I use this method often to convert QStrings into c-strings and its never seg faulted. In what situtation does it seg fault?

  7. #7
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    maybe the confusion here is my semi pseudo code:

    Qt Code:
    1. QString str = "Some string";
    2. const char * c_string = str.toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

    not

    Qt Code:
    1. const char * c_string = QString().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

    I agree putting the latter line into code would probably segfault or return NULL to the pointer. Sorry for the confusion.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    No, that's still wrong. The whole concept of doing what you are doing is wrong.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString use .c_str()?

    Is this the whole "intermediate references smell like poo" thing?

  10. #10
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Well could you explain why you think that? I dont see how its any different from converting to a QBtyeArray and pulling the char * out of it.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    Quote Originally Posted by jonthom View Post
    Well could you explain why you think that? I dont see how its any different from converting to a QBtyeArray and pulling the char * out of it.
    Well, this is wrong too of course:
    Qt Code:
    1. QString str = "xxx";
    2. const char *cstr = str.toAscii().constData();
    To copy to clipboard, switch view to plain text mode 

    This is much better although might still be wrong in some cases:
    Qt Code:
    1. QString str = "xxx";
    2. QByteArray ba = str.toAscii();
    3. const char *cstr = ba.constData();
    To copy to clipboard, switch view to plain text mode 

    This is ok:
    Qt Code:
    1. QString str = "xxx";
    2. QByteArray ba = str.toAscii();
    3. const char *cstr = strcpy(ba.constData());
    To copy to clipboard, switch view to plain text mode 
    So is this:
    Qt Code:
    1. QString str = "xxx";
    2. QByteArray ba = str.toAscii();
    3. someCallThatNeedsACStr(ba.constData());
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Ok i see what you mean now. Yes, if you want a deep copy that you can manipulate safely strcpy() is the way to go. Other than that, this method is effectively the same as using a std::string as the medium. My code was intended for use as an argument to a function that wants a c-str. Also, getting the shallow copy isnt necessarily "wrong" it just depends on what you intend to do with it.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    Quote Originally Posted by jonthom View Post
    Other than that, this method is effectively the same as using a std::string as the medium.
    The problem is that your code uses a temporary object which immediately gets destroyed so the result of c_str() is immediately invalid - it points to a region of memory which might or might not contain the string.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Yes you're right. QString().toStdString() creates a temporary std::string variable with the string value of the QString. The c_str() routine returns a pointer to memory held by that temp std::string not the original QString. I never thought of that but I really only use this method to immediately use the data for a function call. The function you use it on must deep copy the data before any other operation overwrites it. It is a very dangerous statement and can result in a segfault. Perhaps I should rethink using it. Thanks for expounding your opinion.

    ok hows this:

    Qt Code:
    1. #include <string>
    2.  
    3. QString str = "xxx";
    4. std::string stdstr = str.toStdString();
    5. const char * cstr = stdstr.c_str();
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    Quote Originally Posted by jonthom View Post
    I never thought of that but I really only use this method to immediately use the data for a function call.
    If you go through a temporary, the data might already be invalid when you enter the function.

    The function you use it on must deep copy the data before any other operation overwrites it.
    It's already too late. Initialization of the function itself or any of its local variables might overwrite the temporary. Of course unless the temporary is created during the function call:
    Qt Code:
    1. func(str.toStdString().c_str());
    To copy to clipboard, switch view to plain text mode 

    ok hows this:

    Qt Code:
    1. #include <string>
    2.  
    3. QString str = "xxx";
    4. std::string stdstr = str.toStdString();
    5. const char * cstr = stdstr.c_str();
    To copy to clipboard, switch view to plain text mode 
    This is ok as long as stdstr lives.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Ok so I just went back to bjarne. According to him, a temp variable is destroyed at the end of the expression in which it is used. Becuase a function call in an expression is executed as part of the expression, the temp variable memory block will exist and be valid in this context:

    Qt Code:
    1. QString str = "xxx";
    2. (void)somefunction(str.toStdString().c_str());
    To copy to clipboard, switch view to plain text mode 

    so that is correct code.

  17. #17
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Quote Originally Posted by wysota View Post
    It's already too late. Initialization of the function itself or any of its local variables might overwrite the temporary.
    Thats not true if the function call and the temp are in a single statement (see above).

    Quote Originally Posted by wysota View Post
    Of course unless the temporary is created during the function call:
    Qt Code:
    1. func(str.toStdString().c_str());
    To copy to clipboard, switch view to plain text mode 
    That was my point in the first place. I would only use a statement like this during the function call.

    const char * str = QString().toStdString().c_str();

    by itself definately goes out of scope. I was only using that as an example of the syntax

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    Quote Originally Posted by jonthom View Post
    const char * str = QString().toStdString().c_str();

    by itself definately goes out of scope. I was only using that as an example of the syntax
    Try to avoid that. People tend to copy and paste code directly from this forum to their applications without changing it or reading the whole thread that discusses a particular piece of code. And then they will write somewhere that at Qt Centre they gave them code that didn't work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #19
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Quote Originally Posted by wysota View Post
    Try to avoid that. People tend to copy and paste code directly from this forum to their applications without changing it or reading the whole thread that discusses a particular piece of code. And then they will write somewhere that at Qt Centre they gave them code that didn't work.
    So noted. Thanks for the interesting debate.

Similar Threads

  1. With QString create a QString&
    By avis_phoenix in forum Newbie
    Replies: 1
    Last Post: 21st April 2010, 22:05
  2. Replies: 4
    Last Post: 1st February 2010, 14:21
  3. QString to hex
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 21st December 2009, 12:21
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.