Nah Man,
I'm way Ahead in My School.
My Class just started Functions.
I try to solve these questions and was just exploring the possibility of overloaded operators and their use, when I thought of this code.
The surprising part is that similar code(strcat(p, "Hello");) compiles and runs perfectly in TurboC++.
That's Why I asked this.
p is a pointer to a string literal. You haven't created any memory to store that string - it's been assigned by the compiler. When you attempt to concatenate extra text to the end of the string, you're writing to memory you don't own, very probably past the end of the memory allocated for the string. This causes undefined behaviour - and, very likely, a crash.
Seems Legit, thanks
But, then why does the program compile and run in TurboC++.
Is it because its very old and this security measure wasn't implemented till then.
Also how do I Fix this if I Dont want to create an array.
But, then why does the program compile and run in TurboC++.
Undefined behaviour is undefined. Each implementation can do what it wants, as long as it complies with the C++ standards. Who knows what Turbo C++ is doing under the hood?
Also how do I Fix this if I Dont want to create an array.
Use STL strings instead.
If the antediluvian compiler you're using doesn't support them, then you're going to have to allocate memory for a C-style string, one way or another. Either you do it on the stack:
Yes. And also because of C backward compatibility.
Also how do I Fix this if I Dont want to create an array.
Exactly the same way as you would write a program without main() function. Or use cout without including <iostream>
1 2 3 4 5
constint size = 25;
char p[size] = "Hello";
strncat(p, " , How Are You ?", size); // do not use _s versions of functions
//they are nonstandard and incompatible with other compulers
cout << p;
Jl might have been talking about the visual 2012 though I am not sure if it is default 11 or 03 since I don't use visual. I knoow code::blocks is default 03 and you have to change to 11.
Well, question was "Why does it compiles in TurboC++ though it is incorrect". So answer is that it compiles because it still support such compatibility (Becaue it does not know about C++11)
As MiiNiPaa says, the OP was asking about Turbo C++. The stuff about C++11 is a worthwhile digression, but it doesn't answer the OP's question.
The reason it compiles in Turbo C++ is because it compiles under all C++ compilers that conform to an older C++ standard than C++11. Prior to the C++11 standard, it was perfectly legal syntactically.
That's the thing about C and C++ - just because something is legal syntactically, doesn't mean it won't cause your program to crash.
> As MiiNiPaa says, the OP was asking about Turbo C++
No. Read the original question once again. And perhaps this time, you might be able to figure out what the the question is.
Simple Question,
Why Does This Code Throw An Exception in Visual Studio 2012.
Answer: It should not have compiled under Visual Studio 2012 if it was C++11 conforming. However, the compiler appears to be still conforming to C++98 / C++03 standard with respect to this particular construct.
All the stuff about Turbo C++ is an utterly worthless digression into ancient history.
It wasn't answered (at least not answered correctly) as far as I can make out. Unless someone gave a correct answer and then deleted the post for some reason.