Java – undefined behavior

I did some simulations on C and I encountered a strange problem I have the following function to return the vector of double:

vector<double> processSimulation(int Q){
//do things
vector<double> output;
output.push_back(mean);
output.push_back(variance);
return output;
}

In general, I have the following points:

//define Q
vector<double>::iterator it = processSimulation(Q).begin();
double mean = *it;
double variance = *(it+1);

The problem is that I got a wrong number (E - 305) and the correct number of variance I try to explain this behavior myself. I think it may be caused by undefined behavior, because the iterator points to the old vector in the function, which is now out of range and no longer exists Am I right? Maybe I'm just lucky, variance is right, because it can also be wrong

I changed the code to

vector<double> output = processSimulation(Q);
vector<double>::iterator it = output.begin();
//same as before

It works well, so it strengthens my creation

In addition, I noticed that a debugger was strange: when trying to find out what happened (before fixing the code), I checked the values of mean and variance through debugging, and they were all wrong Although, when I run the program, only the average value is wrong (I've tried many times and it's always: it's wrong when debugging, which means the error and variance are correct at run time) What happened here?

Java problem: Well, this problem I encounter really annoys me, because often in Java, in order to shorten things, I do not define a new object, but directly use methods on the functions that return the object (as shown in this example) Although, I have never encountered any problems Have I been doing unconscious things (fortunately)? Or is it just that there is no such behavior in Java, because the functions that should return objects actually return pointers to them, and the real objects are always in the heap (and surrounded when they are not referenced)?

I hope you can clarify my doubts!

Solution

This is a very common mistake people make when linking calls on rvalues instead of storing the results in local variables

vector<double>::iterator it = processSimulation(Q).begin();

Above, your processsimulation (q) call returns vector < double > You will then get an iterator to the beginning of the vector and store it Since the result vector is no longer in range, it will be destroyed This leaves a dangling iterator

Now you start using it Remember that the iterator contains valid information, but it points to an object that no longer exists:

double mean = *it;            // undefined behavIoUr
double variance = *(it+1);    // undefined behavIoUr

Think of it as something like this:

vector<double>::iterator it;
{
    vector<double> result = processSimulation(Q);
    it = result.begin();
}
double mean = *it;    // boom

When you change the code to store the return value in a local variable, the behavior is defined, provided that the vector remains within range throughout the time you use the iterator

So this is correct (except for the comments related to c style on your question):

vector<double> output = processSimulation(Q);
vector<double>::iterator it = output.begin();
double mean = *it;
double variance = *(it+1);

However, you can easily discard iterators and use array index operators:

double mean = output[0];
double variance = output[1];

You may want to consider returning your own structure that encapsulates this information, not a vector Or at least switch to using STD:: pair < double, double >

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>