-2

I came across a problem where I needed to find the sizeof an array to determine the length of an array. My understanding is that a char is 1 byte, a string is 2 bytes (an extra byte for the \0 character).

However, I discovered that a single element of a string array is 24 bytes.

I outputted the size of a string, char, and int variables to verify my understanding. I did the same for a single element of an int, char and string arrays.


#include <iostream>

using namespace std;


int main()
{
    string strArr[1] = {" "};

    cout
        << sizeof " " << endl
        << sizeof strArr[0] << endl
        << sizeof strArr << endl << endl;

    char charArr[1] = {'a'};

    cout
    << sizeof 'a' << endl
    << sizeof charArr[0] << endl
    << sizeof charArr << endl << endl;

    int intArr[1] = {1};

    cout
        << sizeof 1 << endl
        << sizeof intArr[0] << endl
        << sizeof intArr << endl;

    return 0;
}

Expected results:

2

2

2

1

1

1

4

4

4

Actual results:

2

24

24

1

1

1

4

4

4

awebber
  • 21
  • 2
  • 1
    `string strArr[1]` is an array of 1 `std::string`. `strArr[0]` is a `std::string`, not a `char`. And finally, the `sizeof` a `std::string` is constant and does not reflect the size of the value it contains. – François Andrieux Apr 30 '19 at 20:33
  • Use `.size()` to get size of a `std::string`. Use `sizeof` to get the amount of `char`s in a `char` array. – HolyBlackCat Apr 30 '19 at 20:36
  • You should first read a little bit on strings. Size of a string depends on the number of its characters and you can get it by str.length(). If you use a char array instead, you should add '\0' to the end of the array, which gives you +1 to the number of characters. – Can Bayar Apr 30 '19 at 20:37
  • @HolyBlackCat use `std::size` rather than `sizeof` to get the length of an array 1. so that you (not specifically) don't accidentally get the size of a pointer which you thought was an array but haven't learned about adjustment of function parameters and 2. so that you don't need to consider the size of the element (in case you happen to use `wchar_t` or `char32_t` later). – eerorika Apr 30 '19 at 20:54
  • `sizeof` operator gives the size of the object (array/struct/class/etc.), not its contents. – Cruz Jean Apr 30 '19 at 20:54
  • On my Ubuntu 18.04 64 bit desktop and using g++ v7: With std::string s; the sizeof(std::string) is 32 bytes, regardless of s.size(). The std::string object size is an implementation issue (which could change for each compiler update). – 2785528 Apr 30 '19 at 22:46

1 Answers1

4

Why is sizeof array(type string) 24 bytes with a single space element?

Because the size of a std::string object is 24 bytes (on your system).

My understanding is that a char is 1 byte

Correct.

a string is 2 bytes (an extra byte for the \0 character).

An array of 2 char objects is indeed 2 bytes.

However, I discovered that a single element of a string array is 24 bytes.

You seem to be conflating different meanings of the word "string". While a string is indeed an array of characters, std::string is not just an array of characters. Rather, std::string is a class that represents a string.

More specifically, std::string is a class that manages a dynamically allocated array of characters. As such, the size of a string must be at least the size of a pointer that points to the allocated memory.

eerorika
  • 181,943
  • 10
  • 144
  • 256
  • 1
    Addendum: [Some reading on what else might be in that `string`.](https://stackoverflow.com/questions/10315041/meaning-of-acronym-sso-in-the-context-of-stdstring) – user4581301 Apr 30 '19 at 20:44