Reputation: 31
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
int len ;
strcat( str1, str2);
len = strlen(str1);
cout << len << endl<<str1<<endl;
return 0;
}
When i initialize str1 like this char str1[5]="hello";
, C++ compiler gives me an error (means the size of the string must be 1 more than the given string).
But in the strcat
we are concatenate str1
with str2
with the size of the first string is 10 and length of the string is also 10 (means no extra null character) . Why is this changing behavior ?
Are we converting C++ string with C string in strcat
function, please explain.
Upvotes: 1
Views: 77
Reputation: 1
These are C-style strings, not C++ strings, i.e., you are not using the std::string class. C-style strings are terminated by '\0', which means you need an extra storage location beyond the length of the string. "Hello" has a length of 5, but you need an array of size 6 to store it. In your example, it is not actually necessary to specify the size of the arrays, just do the following and let the compiler calculate the size:
char str1[] = "Hello";
The strcat function requires that the first argument points to an array that has sufficient space for the concatenated string. It is a dangerous function because it will not check whether this is actually the case, if not, it will simply overwrite whatever is beyond the end of your array. In your case, concatenating two strings that are each 5 characters long, you will get a string that is 10 characters long, and you need to make sure that the first argument points to an array that is at least 11 characters long. If you were using C++ strings you would not have to worry about any of this:
#include <string>
#include <iostream>
int main()
{
std::string str1("Hello");
std::string str2("World");
str1.append(str2);
std::cout << str1.size() << '\n' << str1 << std::endl;
}
Upvotes: 0
Reputation: 238351
Are we converting c++ string
No, there are no c++ strings (i.e. std::string
) involved in your program.
why is this changed behavior ?
You wrote a different program, and the behaviour is different.
In the first case, your program was ill-formed. The standard requires the compiler to notify you of this at compile time. And detecting your bug is easy for the compiler as well. The type of the variable is "array of 5 characters". 6 characters won't fit - the type is wrong. Types are known at compile time.
In the example code, you use strcat
. If you take a look at this reference you'll find that
The behavior is undefined if the destination array is not large enough for the contents of both src and dest and the terminating null character.
Standard doesn't require the compiler to warn you about undefined behaviour. In fact, it is usually quite difficult for the compiler to prove that a program has undefined behaviour - even though in this particular case it seems obvious.
The arguments of strcat
are pointers. You pass pointers of correct type to strcat
. There are no type errors. The bug is in the wrong values. The values are copied into the arrays at run time (unless the optimizer does some magic). To figure out your bug, the compiler would have to simulate the execution of the program. Doing that for all code paths would be extremely slow. So, the compiler leaves the responsibility to the programmer to satisfy the preconditions of strcat
.
Upvotes: 2