Case Study on cross-border problem of C ++ character array

Source: Internet
Author: User

Instructor he:


Instructor, m_szDepartment = new char [strlen (department) + 1]; why do we need to add 1? After removing + 1 in the test, the effect is the same. I don't quite understand it ...... Please advise ......
Student, Yang tengfei

My answer:
You need to assign blocks to '\ 0. I saved my own information using another person's website (cross-border part). The running results will be different if someone else wants to use it. This is the most dangerous problem.

He continued to ask:
However, when defining character arrays, such as a [4], you can enter five characters. Is the '\ 0' position of this array also occupied by others?

I am excited about the problem of this clever classmate. I should not just read the program but ask questions. If you can raise your own questions, you will be able to learn well. The teacher answers the question, asks a new question, answers it by himself, or asks the teacher again. This is the communication. I am proud to have raised a good question for the students. My answer is:

The question you asked is not average. Please refer to my new blog post to discuss this question.

 


The following is my answer to this question. Try a program to solve the problem. The program is:


[Cpp]
# Include <iostream>
Using namespace std;
Int main ()
{
Char a [4];
Cin>;
Cout <a <endl;
Return 0;
}

# Include <iostream>
Using namespace std;
Int main ()
{
Char a [4];
Cin>;
Cout <a <endl;
Return 0;
} Dear reader, please do not just read this article to open a familiar programming environment and run it while reading it. What do you find?

Enter abcd and press Enter. The output is abcd. Cout <a outputs character arrays as strings. Obviously, abcd is full of its own location a [0] to a [3] and can be output as expected, actually, the occupied memory a [4] unit is occupied. Of course, the face is '\ 0' at exactly a [4 '. If "hot" do not have to be surprised.

Run again and enter abcde. The result I run is that in VC ++ 6.0, the abcde is output, and a memory out-of-bounds error message is displayed. Under codeBlocks, output abcde and there is no prompt.
Please think about how dangerous this is.
A program is provided below:


[Cpp]
# Include <iostream>
Using namespace std;
Int main ()
{
Char a [4], B [4];
Cin>;
Cin> B;
Cout <a <endl;
Cout <B <endl;
Return 0;
}

# Include <iostream>
Using namespace std;
Int main ()
{
Char a [4], B [4];
Cin>;
Cin> B;
Cout <a <endl;
Cout <B <endl;
Return 0;
} The running task is handed over to the reader. If you enter 3 characters, 4 characters, and 5 characters, you can try it on multiple platforms. It is strongly recommended to track data storage in the memory by means of one-step execution.

The following are three characters (abc and hij) for Array a and array B. The result is displayed in one step:

  
 


The following figure shows the result after entering 5 characters (abcef and hijkl) for Array a and array B:



  


From this we can see that in VC ++ 6.0, the address of the first defined array a is greater than the address of the second defined array B. Originally, abcde was entered for Array a, occupying others' territory, then, input hijkl for B, which encroaches on a's territory. B [4] means that a [0] is l, B [5] is a [1], store '\ 0 '!

It is the result of debugging with the same input in codeBlocks. The result is the same:

  

Next, let's change the order of input a and input B to another program:


[Cpp]
# Include <iostream>
Using namespace std;
Int main ()
{
Char a [4], B [4];
Cin> B;
Cin>;
Cout <a <endl;
Cout <B <endl;
Return 0;
}

# Include <iostream>
Using namespace std;
Int main ()
{
Char a [4], B [4];
Cin> B;
Cin>;
Cout <a <endl;
Cout <B <endl;
Return 0;
} What is the running result? Let's talk to you. Don't forget. Use the debugging tool to relieve your doubts.


 

Related Article

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.