Pointer Exception Error

Simple Question,
Why Does This Code Throw An Exception in Visual Studio 2012.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

void main()
{
	char *p;
	p = "Hello";
	strcat_s(p, 25, " , How Are You ?");
	cout << p;
	cin.get();
}
Is this a homework question?
@MikeyBoy

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.
Last edited on
@MikeyBoy

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:

char myString[SOME_NUMBER_BIG_ENOUGH];

or you do it dynamically, on the heap:

char* myString = new char[SOME_NUMBER_BIG_ENOUGH];
@MikeyBoy

Thanks For Helping.
BTW This Truly isn't a HW Question.
You're welcome :)
Is it because its very old
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
const int 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;
> And also because of C backward compatibility.

This compatibility feature has been removed in C++11.
The type of a string literal is changed from “array of char” to “array of const char.”

char* p = "abc"; // valid in C, invalid in C++ - IS
JLBorges wrote:
C++11
Yash8976 wrote:
TurboC++
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.
> TurboC++

Is utterly irrelevant to what I was pointing out.

Repeat: This compatibility feature has been removed in C++11.

It was allowed (for backward compatibility with C), but deprecated, in C++98, C++03.

It is no longer allowed in C++11 (even though many compilers still allow it to pass with a deprecated warning).
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.
We were replying to the second question:
http://www.cplusplus.com/forum/beginner/112831/#msg616384

First one was answered already.
So we spoke abour different questions :)
> First one was answered already.

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.
Topic archived. No new replies allowed.