James MV
James MV

Reputation: 8717

Why the difference in size when declaring a string in C++?

I should know this, but I don't and I think its probably a major gap in my foundation knowledge so I thought I should ask the experts.

Given:

 char s1[] = { 'M', 'y', 'W', 'o', 'r', 'd' };
 char s2[] = "MyWord";

cout << strlen(s1)<<endl;
cout << strlen(s2)<<endl;

cout << sizeof(s1)<<endl;
cout << sizeof(s2)<<endl;

Why when declared as s1 is the strlen 9 but when declared as s2 is is 6? Where does the extra 3 come from, it it the lack of a null terminating character?

And I understand that sizeof(s2) is 1 byte larger than sizeof(s2) because s2 will have the null character automatically added?

Please be gentle, TIA!

Upvotes: 1

Views: 159

Answers (6)

Andrew T Finnell
Andrew T Finnell

Reputation: 13628

char s2[] = "MyWord"; Auto adds the null terminator because of the "" declaration.

s1 declaration does not. When you do a strlen on s1 and it comes out to 9 it is because it eventually ran into a \0 and stopped. s1 shouldn't be used with strlen since it is not null terminated. strlen on s1 could have been 1,000. If you turn on memory violation detection strlen of s1 would crash.

Upvotes: 7

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361292

In your code,

  • s1 and s2 are arrays of char.
  • s1 last element is d, whereas s2 last element is \0.

That is, there is one more character in s2. It is a null-terminated string but s1 is not a null-terminated string.

Since s1 is not a null-terminated string, the expression strlen(s1) would invoke undefined behavior, as strlen will continue reading s1 beyond the last element.

Upvotes: 1

Roee Gavirel
Roee Gavirel

Reputation: 19445

strlen(s1) can return any value > 6. because he is searching for the first '\0' char and you didn't provide it.

Upvotes: 1

tzerb
tzerb

Reputation: 1433

s1 is only 9 by happenstance; you can only use strlen on terminated strings. Declare it as char s1[] = { 'M', 'y', 'W', 'o', 'r', 'd', '\0' }; and see what happens.

Upvotes: 2

adl
adl

Reputation: 16034

Lack of terminating null character as you say.

When the strlen function is called on s1, it counts the characters until it finds a terminating '\0'. You may have different result depending on how your memory is initialized.

Your definition of s2 is actually equivalent to

char s2[] = { 'M', 'y', 'W', 'o', 'r', 'd', '\0' };

Upvotes: 2

cnicutar
cnicutar

Reputation: 182619

The first one lacks the implicit \0 terminator present in the second. Thus:

  • The first is 1 less than the second, memory-wise
  • Doing strlen on the first is undefined behavior (since it lacks the terminator)

Upvotes: 7

Related Questions