In C ++ How do I compare the size of a character array with a string?

I have the following ad

char c[] = "Hello";
string c2 = "Hello";

      

I want to compare a) how many bytes of memory are needed, and b) the lengths of the characters. I know that character arrays add a null terminator at the end of the string where there are no string datatypes.

Using

cout << "The sizeof of c: " << sizeof(c);
cout << "The sizeof of c2: " << sizeof(c2);

      

returns 6 and 4 and I'm not sure why 4 and not 5? also how the length function is compared here ...

When I use the following code

cout << "The sizeof of c: " << sizeof(c);
cout <<"The sizeof of c2: " << c2.length();

      

I get 6 and 5 ... but are they comparing the same thing? Thank.

+3


source to share


6 answers


a) how many memory bites are needed and

You used the sizeof operator correctly to determine how many bytes a character array occupies.

it

sizeof( c )

      

As far as the type object is concerned std::string

, it occupies two extents of memory. The first is used to highlight the object itself, and the second is used to highlight the line held by the object.

So

sizeof( c2 )

      

will give you the size of the memory occupied by the object.



c2.capacity()

      

will give you the size allocated to the object to hold the string and maybe some additional characters to be filled in the future.

When I use the following code cout <<"Size c:" <SizeOf (c); cout <". Size c2:" <c2.length ();

I get 6 and 5

If you want to compare strings themselves without a null-terminated character array, then you should write

cout << "The length of c: " << std::strlen(c);
cout <<"The length of c2: " << c2.length();

      

and you get the result 5 and 5.

You can do the following experiment with std :: string objects.

std::string s;

std::cout << sizeof( s ) << '\t' << s.capacity() << '\t' << s.length() << std::endl;

std::string s1( 1, 'A' );

std::cout << sizeof( s1 ) << '\t' << s1.capacity() << '\t' << s1.length() << std::endl;

std::string s3( 2, 'A' );

std::cout << sizeof( s2 ) << '\t' << s2.capacity() << '\t' << s2.length() << std::endl;

std::string s3( 16, 'A' );

std::cout << sizeof( s3 ) << '\t' << s3.capacity() << '\t' << s3.length() << std::endl;

      

+8


source


sizeof(c)

is the size of the array, which contains five characters in the literal expression with which you initialize it, plus a null-terminated terminator at the end, giving a total of six bytes.

sizeof(c2)

is a class size string

that doesn't tell you anything particularly useful. The class manages dynamically allocated memory containing string characters; which is not part of the object itself string

.



c2.length()

- the number of characters in the line controlled c2

; five characters.

+3


source


a) how many bytes of memory are required, and b) character lengths

  • the variable 'c' uses 6 bytes on the stack (5 letters and a null terminator)

  • sizeof (c) = 6, strlen (c) = 5

  • Required memory bytes: 6


  • If 'c' had 1000 characters,

    • sizeof (c) = 1001, strlen (c) = 1000)
  • Total required memory bytes: 1001


  • the variable 'c2' uses 4 bytes on the stack (I suspect a pointer, but not confirmed), and at least 5 bytes elsewhere (I think a heap).

  • sizeof (c2) = 4, c2.size () = 5, strlen (c2.c_str ()) = 5

  • Required memory bytes: 9+ (4 + 5) +


  • if 'c2' had 1000 characters, i.e. c2.size () == 1000

  • 4 bytes on the stack and

  • at least 1000 bytes elsewhere (depending on the implementation, maybe a few more)

  • Total bytes of memory: 1004 +

NOTE. std :: string is a container. I think no such values ​​are specified and should be considered implementation dependent.

+2


source


I am assuming size [] includes the terminating null character, so 5 + 1 = 6 bytes.

The size of the string object returns 4 bytes, which is probably the size of the pointer that points to the string object. 32 bit.

In the latter case, you use a length that is programmed to count the number of characters.

+1


source


Well the size c[] = "Hello"

is 6 because the char array should allocate 1 more byte of memory for the null character \0

.

The function length()

returns the number of characters in a string literal. The count does not include the null character \0

.

0


source


std::string

for example std::unique_ptr

, std::shared_ptr

and std::vector

, is a smart pointer and some additional manipulation member functions.

When you pass one on sizeof

, you are measuring the size of the smart pointer - sizeof (std::string)

not the content.

0


source







All Articles