How many bytes in a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lifehacker
    New Member
    • Aug 2008
    • 1

    How many bytes in a string?

    Hi,
    I am a total n00b in C++ and have a basic question:

    How big is a string:

    I have discovered the following:

    bool - 1 byte
    char - 1 byte
    int - 2 bytes
    short int - 2 bytes
    long int - 4 bytes
    float - 4 bytes
    double - 8 bytes

    then theres string... does it have a fixed size?

    and what about enum? Its a type as well, isnt it?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    A C++ string object can be implemented in various ways. Usually you have to use a method on the string object to get its size. Size of a string is usually the number of characters (not bytes) in the string. That means you can't use sizeof since sizeof only reports the memory occupied by the variable on the stack frame.

    A C string is a char array with a binary zero (\0) as the final char. In this case you use strlen() tp get the number of characters. Here, again, sizeof will not work unless the array is local to the function or is a global array.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by lifehacker
      I have discovered the following:

      bool - 1 byte
      char - 1 byte
      int - 2 bytes
      short int - 2 bytes
      long int - 4 bytes
      float - 4 bytes
      double - 8 bytes
      The only one of these statements that is actually correct is

      char - 1 byte

      This is guaranteed by the C and C++ standards. A lot of your other statements are true in many many cases but the size of most types is actually platform dependent.

      So if you had said "I have discovered the following about my platform" then you would have been correct (possibly).

      I am surprised you find int to be 2 bytes. Since you are new to C++ programming I would image you are using a PC and most modern compilers give an int of 4 bytes on a PC.

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        As weaknessforcats said, using the sizeof operator, like this:
        sizeof(string)
        will give you the amount of space that a string object takes up on the stack on your particular platform, that is, the amount of space that string objects are guarunteed to take up, even before you put any characters into them. But string objects, unlike the other types you have been finding the size of, have space allocated on the heap, which is where the characters are stored. There is no easy way of finding out how much space a string object is taking up on the heap, and it can change while your program is running, as you add and remove characters from the string.

        enum is not really a type. It's more of a way of defining your own types. However, when you declare a variable to be of a type that you have defined with enum, I think it's the same size as an int.

        Did you find out the sizes in your table with the sizeof operator? If you didn't, try using the sizeof operator on all those types to see if it matches what's in your table. Then you can find out what the sizes of strings and enum types are too.

        Hope this helps.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by boxfish
          enum is not really a type. It's more of a way of defining your own types. However, when you declare a variable to be of a type that you have defined with enum, I think it's the same size as an int.
          Size of enum is platform dependent. Some platforms will chose the smallest integer type necessary to hold the defined values of the enums members, others will make it an int unless 1 or more defined values of its members wont fit in an int in which case a long might be used.


          Another thing to remember is that int short, long char, float double etc are built in types of the language. string is not a built in type it is a class defined in the class library which is why they have to be handled differently.

          Comment

          • codebanger
            New Member
            • Dec 2015
            • 1

            #6
            I know it's late but:
            Code:
            /*
                Question came up: "How big is a string character?"
                sizeof returns 8 bytes in all cases except for
                sizeof( str3[1] ) which returns 1
            */
            
            #include <iostream>
            #include <string>
            
            using namespace std;
            
            int main()
            {
                string str;
                int istr = sizeof(str);
                cout << "Empty string is " << istr << " bytes long" << endl;
                string str2(1, 'a');
                istr = sizeof(str2);
                cout << "1 character string is " << istr << " bytes long" << endl;
                string str3("the quick brown fox");
                istr = sizeof(str3);
                cout << "the quick brown fox string is " << istr << " bytes long" << endl;
                istr = sizeof(str3[1]);
                cout << "sizeof str3[1] is " << istr << " bytes long" << endl;
            
                cout << "Hello world!" << endl;
                return 0;
            }

            Comment

            • Jamestwell
              New Member
              • Nov 2025
              • 5

              #7
              Originally posted by Banfa
              Size of enum is platform dependent. Some platforms will chose the smallest integer type necessary to movie box hold the defined values of the enums members, others will make it an int unless 1 or more defined values of its members wont fit in an int in which case a long might be used.


              Another thing to remember is that int short, long char, float double etc are built in types of the language. string is not a built in type it is a class defined in the class library which is why they have to be handled differently.
              In C++, a `std::string` does not have a fixed size because it’s a dynamic object that manages memory internally to store character data. The size of the string object itself (the metadata like pointers, length, capacity, etc.) is typically a few bytes depending on the compiler and system architecture, but the actual character data is stored separately on the heap and can grow as needed. Similarly, `enum` types usually take the size of an `int`, though it can vary by compiler.

              Comment

              • marykline
                New Member
                • Dec 2025
                • 2

                #8
                Having a great and amazing blog post!

                Comment

                Working...