Take the following program:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char a[8] = "Hello, ";
char b[7] = "world!";
strcat(a, b);
cout << a;
return 0;
}
Notice that a
and b
have the same size as their assigned strings.
The documentation states that for strcat(a, b)
to work, a
needs to be large enough to contain the concatenated resulting string.
Nevertheless, cout << a
displays "Hello, world!"
. Am I entering undefined behavior?
"Am I entering undefined behavior?"
Yes. A region at the end of a[] has been written over. It worked, this time, but might have belonged to something else.
Here I use a struct to control memory layout, and demonstrate it:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
struct S {
char a[8];
char b[5];
char c[7];
};
S s;
strcpy( s.a , "Hello, " );
strcpy( s.b , "Foo!" );
strcpy( s.c , "world!" );
strcat(s.a, s.c);
cout << s.a << endl;
cout << s.b << endl;
cin.get();
return 0;
}
This outputs:
Hello, world!
orld!
Instead of:
Hello, world!
Foo!
The strcat() has stomped all over b[].
Please note that in a real life example such bugs may be far more subtle, and lead you to wondering why perfectly innocent function calls 250 lines later crash and burn horribly. ;-)
EDIT: May I also recommend that you use strcat_s, instead? Or, even better, std::strings:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string a = "Hello, ";
string b = "world!";
a = a + b;
cout << a;
}
Am I entering undefined behavior?
Yes.
If the documentation says "a
needs to be large enough to contain the concatenated resulting string", why don't you simply believe it? What is there to doubt?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With