If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

If that (seemed to) work under VC++ 6 it was pure luck. It has been invalid there as well.

P2 is of type char which means that it can hold exactly one character, but you copy the string "test" into it which needs 5 chars to be stored (the actual string plus the '\0' terminator). This is clearly an out-of-bounds access, IOW a memory overwrite, and the error message you get is meant to catch exactly that.

To make it work you would need to make P2 a char array which is able to hold at least that much characters, i.e. define it like that:

Code:

char *P1="test" ,P2[5];

You then would need to remove the & from the calls to Test() and MessageBox().

HTH

BTW, this is no C++/CLI (AKA Managed C++) which the forum here is dedicated to, it is native C++ using MFC which is meant to go into the Visual C++ Programming forum. (The error is not MFC-related however, so the Non Visual C++ forum would have been appropriate for this post as well, but you probably didn't know that when making the post.)

Please use code tags when posting code.

Ah, and... Welcome to CodeGuru!

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

why can't i use the method as code below ?
May i know why it give me the run-time error too ?

While your code from post #1 didn't work simply because the variable P2 was too small, this code as-is is even worse: As long as you don't allocate memory and have the pointer point to it, the pointer practically points to nothing at all, causing a memory overwrite if you only assign a single character to *P2.

Your new code would work if you allocate an array of chars and assign its address to the pointer, like with this line:

Code:

P2 = new char[5];

But then you'd need to de-allocate the memory with the following statement before OnOK() finishes or your code would leak memory:

Code:

delete [] P2;

It is generally not recommended to use dynamically allocated memory unless it's definitely necessary, because it over-complicates code and makes it error-prone. And your sample code is definitely not one of the cases where it's necessary.

Last edited by Eri523; January 17th, 2011 at 07:31 PM.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

So this mean is only have one way to use for the P2 right ?
in other word, it can only use array to complete the work .

No. As we're talking about C++ it's obviously preferable to use an OOP approach and avoid low-level C-style char arrays. The C++ standard library way of doing that is std::string, while the ATL/MFC way is CString. The former of those is cross-platform while the latter is MS-specific.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.