Weird memory problems

Source: Internet
Author: User
Tags valgrind

Running the program, the most strange thing is the memory problem. (For personal experience problems, I only encounter this level for the time being. If there is still a strange degree, please tell me) the memory problem is like a meteor outside of the sky, I don't know when it will come, I don't know how its symptoms are, erratic until the program crashes.

Today we met an example.

void fun(){   vector<double> v1;   vector<double> v2;   process();   ........}

In a function, two vectors V1 and V2 are affirmed, and balabala is then declared until the fun scope is exited and V1 is parsed.

I tried to change the declarative sequence of V1 and V2, the symptoms changed, and an error occurred during the analysis of V2. Then I tried to set V1 and V2 as static variables, no error is reported, but a similar problem occurs in other functions after the fun function is generated.

Now, we can draw conclusions. This is a typical memory problem. It may be that array access is out of bounds, or memory leakage.

Why can you come to this conclusion? We can analyze it as follows:

First, it is not the V1 or V2 structure error, but the first declarative variable structure error. How did this inspire you? Variables declared in the function body (non-static, and not the memory space applied by New) are all local variables, and the program is managed by stacks. When the statement is made, push the data to the stack one by one. When you exit the scope, pop the data to the stack one by one and call the destructor of the variable itself. An error occurs when the POP arrives at the last one, indicating that the stack has been messed up and that the variable's destructor cannot be correctly called Based on the address.

Why not report an error when using static variables? It is very simple that static is global and will not be destructed when exiting the function scope.

Next, we use the binary method to locate and find the problem in the process () function. But there are hundreds of rows in this function. How can I check it.

At this time, valgrind can only be used for help (I have never heard of valgrind's Google ). Finally, I found an array access out of the border. I went there, and I was busy for a long time to make this mistake ......

The problem is solved, but what can we do to ensure that similar problems will not happen in the future?

C ++ does not check whether array access is out of bounds. What should we do.

One idea is to reload () for a custom data structure and perform subscript operations, first assert to see if the subscript is out of bounds, but for a built-in array or STL container, I don't know how to check it during compilation or provide a clear error message when an error occurs.

If you have any experience in this area, please do not hesitate to give me some advice!

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.