Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved How much memory to allow for a 30 character string?

    General and Desktop
    3
    3
    294
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Guerrian
      Guerrian last edited by Guerrian

      I see that sizeof returns 8 for a QString:

      QString s;
      s = "012345678901234567890123456789";
      qDebug() << sizeof(s);
      

      How much more memory is taken up by the characters, is it 1 byte per character?

      Linux Mint 18.3
      Qt 5.14.1
      Qt Creator 4.11.1

      1 Reply Last reply Reply Quote 0
      • M
        mpergand last edited by mpergand

        Hi,

        sizeof return the size of an object:

        qDebug() << sizeof(QString); // -> 8
        

        A QString is composed of 16 bits unicode characters.
        Hence the size in memory is equal to:

        2*s.size()
        or
        sizeof(QChar)*s.size();
        
        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          @Guerrian out of curiosity, are you only interested in the space taken by the chars or the complete data structure that is QString ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 1
          • First post
            Last post