none
Sizeof vs Length vs Capacity in C++ RRS feed

  • Question

  • Hello,

    What is the differences between Sizeof vs Length vs Capacity in C++?

    In the below example:

    Length() is 12.

    Sizeof() is 28 and capacity is 15.

    #include <iostream> 
    #include<string>
    using namespace std;
    
    int main()
    {
        string s;
        getline(cin, s);
        int sl = s.length();
        int so = sizeof(s);
        char* p = new char[sl];
        for (int i = 0; i < sl; i++)
        {
            p[i] = s[i];
        }
        for (int i = 0; i < sl; i++)
        {
            cout << p[i] << " ";
        }
        return 0;
    }

    Tuesday, December 10, 2019 1:44 AM

Answers

  • Hello,

    Thank you for posting here.

    >>What is the differences between Sizeof vs Length vs Capacity in C++?

    length(): It returns the length of the string in bytes. It is the actual number of bytes that match the contents of the string and is not necessarily equal to its capacity.

    capacity(): It returns the size of the allocated storage, it is the size of storage allocated for the string(in bytes). The capacity is not necessarily equal to the string length. It can be equal or larger, the object can optimize with extra space when new characters are added to the string.

    sizeof(): It returns the bytes of memory occupied by an object or type. In your case, string may contain not only chars, but also other non-static variables or virtual function table. These also occupy memory space. The return of sizeof() will include all these memory spaces.

    Best Regards,

    Suarez Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, December 10, 2019 2:29 AM

All replies

  • Hello,

    Thank you for posting here.

    >>What is the differences between Sizeof vs Length vs Capacity in C++?

    length(): It returns the length of the string in bytes. It is the actual number of bytes that match the contents of the string and is not necessarily equal to its capacity.

    capacity(): It returns the size of the allocated storage, it is the size of storage allocated for the string(in bytes). The capacity is not necessarily equal to the string length. It can be equal or larger, the object can optimize with extra space when new characters are added to the string.

    sizeof(): It returns the bytes of memory occupied by an object or type. In your case, string may contain not only chars, but also other non-static variables or virtual function table. These also occupy memory space. The return of sizeof() will include all these memory spaces.

    Best Regards,

    Suarez Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, December 10, 2019 2:29 AM
  • From https://docs.microsoft.com/en-us/cpp/standard-library/basic-string-class?view=vs-2019

    basic_string::capacity

    Returns the largest number of elements that could be stored in a string without increasing the memory allocation of the string.

    basic_string::length

    Returns the current number of elements in a string.

    The C++ Standard Library provides specializations of this class template, with the type definitions string for elements of type char, wstring, for wchar_t, u16string for char16_t, and u32string for char32_t.

    To see the storage allocation used by VC++ for these data types refer to https://docs.microsoft.com/en-us/cpp/cpp/char-wchar-t-char16-t-char32-t?view=vs-2019

    ---------------------------------------------------------------------------------

    Note that the member functions are defined to return a number of elements, If the string object holds an element type other than char, these member functions will not return byte values.

    • Proposed as answer by Guido Franzke Wednesday, December 11, 2019 7:08 AM
    Tuesday, December 10, 2019 4:51 AM