Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How much memory to allow for a 30 character string?
Qt 6.12 LTS is out! See what's new in the release blog

How much memory to allow for a 30 character string?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 1.1k Views 2 Watching
  • 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.
  • GuerrianG Offline
    GuerrianG Offline
    Guerrian
    wrote on last edited by Guerrian
    #1

    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
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      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
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved