CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2011
    Posts
    4

    Run-Time Check Failure #2 - Stack around the variable 'P2' was corrupted.

    I have a code that working in the VC6++ but it give the error in VC9

    the error message is :-
    "Run-Time Check Failure #2 - Stack around the variable 'P2' was corrupted."

    My question is,
    how to change code below to suit the VC9 with still using the Pointer?


    Below is my Code : -
    void CFunctionDlg::Test(char *One,char *Two)
    {

    char *show ="two";

    while(*Two++ = *One++); //-----> error cause by here.

    return ;
    }

    void CFunctionDlg::OnOK()
    {

    char *P1="test" ,P2;


    Test(P1,&P2);

    MessageBox (&P2);

    }

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Run-Time Check Failure #2 - Stack around the variable 'P2' was corrupted.

    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.

  3. #3
    Join Date
    Jan 2011
    Posts
    4

    Re: Run-Time Check Failure #2 - Stack around the variable 'P2' was corrupted.

    thank Eri523 , your method work. but i still have a question.

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


    void CFunctionDlg::Test(char *One,char *Two)
    {
    int i;
    char *show ="two";


    while(*Two++ = *One++);
    return ;
    }

    void CFunctionDlg::OnOK()
    {

    char *P1="test" ,*P2; // using P2 as pointer.

    Test(P1,P2);
    MessageBox (P2);

    }

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Run-Time Check Failure #2 - Stack around the variable 'P2' was corrupted.

    Quote Originally Posted by hahiho View Post
    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 08: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.

  5. #5
    Join Date
    Jan 2011
    Posts
    4

    Re: Run-Time Check Failure #2 - Stack around the variable 'P2' was corrupted.

    Thank for the detail explanation.

    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 .

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Run-Time Check Failure #2 - Stack around the variable 'P2' was corrupted.

    Quote Originally Posted by hahiho View Post
    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.

  7. #7
    Join Date
    Jan 2011
    Posts
    4

    Re: Run-Time Check Failure #2 - Stack around the variable 'P2' was corrupted.

    the purpose of this code is want to get more understand on the pointer. This is the reason i keep asking on the pointer.. but i like your explanation..

    because i am always confusing on the C++ and C. and now at least i go better picture on the different between low level C , and C++

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured