Run-Time check failure #2-stack around the variable 'var' was upted

Source: Internet
Author: User

 

Run-Time check failure #2
Generally, the stack is damaged. Your Code There may be a buffer overflow problem.

Run-Time check failure #2-stack around the variable 'var' was upted
Abstract:
Introduction to new functions of VC ++. Net compilation, runtime error check, compilation options (/rtc1, equiv. To/rtcsu)
Recently, when someone else transplanted a service written under VC 6 to VC 7.1 (. NET 2003 ),
Some running error messages.
After querying the compiler and error description, we found this new feature of the compiler and corrected the code risk:
// Someheadefile. h
Typedef struct tarsomemessagepack
{
Char moiblenumber [20];
....
} Somemessagepack_t, * somemessagepack_ptr;

Careful developers may have discovered that the subscript of the string array is out of bounds. That is, mobilenumber [] valid Element
The subscript is 0 to 19, and mobilenumber [20] is invalid. In VC 6, compilation and running are not subject to any problems.
The lenumber [] is also related to the memory-byte alignment. In fact, the mobilenumber [] is compiled in a default 8-byte alignment environment.
Therefore, the last four bits will not be used by the system, that is, mobilenumber [20], mobilenumber [21],
Mobilenumber [22] And mobilenumber [23] are empty.
Although the system runs normally in the same years
Strncpy (pack. mobilenumber, "", 20 );
Such code is also dangerous. If one day, change the byte alignment of the struct to 1ProgramYes
Crash here.
Vc7.1 provides a mechanism for us to add (/rtc1, equiv. To/rtcsu) items in the compilation options. When the program
When running here, this error will be reported to us.
After reading the relevant documentation and understanding the real intention of the code, I changed the code
Strncpy (pack. mobilenumber, "", 19 );
This problem disappears.

// Someimplement. cpp
Strncpy (pack. mobilenumber, "", 20 );

Zheng Note: This is caused by memory access errors of some variables. In the error dialog box, it is obvious that the variable access error is proposed. You can check this variable to get the result. Typical error: If a tchar code [1] is defined, code [1] is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.