开发者

Pointer is Affected on Function Call Where it Shouldn't Be

I am calling the function below, which calculates a value given in the passed in variables. However, when the function (calculate_distance) runs, the data contained in the variable (loc_ptr) that is passed into the function seems to change.

I have stepped through the code to find that the variable loc_ptr is affected on the function call, rather than inside the function itself.

The function is being called in a while loop. The first iteration of the wh开发者_如何学Goile loop calculates correctly, it is only on subsequent iterations that the problem seems to occur.

_Note: location is a typedef'ed struct containing char *name, double latitude, double longitude. options is an array of doubles._

double calculate_distance(location from, location to) {
    return to.latitude - from.latitude;
}

main() {
    location current_location = {"Plymouth", 50.378565, -4.134339};
    location locations[3] = {{"Padstow", 50.5384, -4.9378}, 
                            {"Newquay", 50.412, -5.0757}, 
                            {"Boscastle", 50.684, -4.6929}};

    // create a pointer to an array of locations.
    location* loc_ptr;
    loc_ptr = &locations[0];

    double options[3];
    int i = 0;
    int position = 3;

    while (i < position) {
        // calculate the distance between the current and other locations
        options[position] = calculate_distance(current_location,
                                                loc_ptr[position]);
        position--;
    }
    // handle the rest of the algorithm
}

Note: The code is part of a larger algorithm, it's cut short as it would be quite long. After the while loop finishes, the loc_ptr array is rebuilt to remove one of the elements.


Stack corruption? check sizes of options[] locations[] arrays. It's also good to use asserts to avoid running out of bounds.

So, now I see problem with indexes. locations[] array has 3 elements, but you access the forth during the first loop (loc_ptr[position] -> loc_ptr[3]), the same with options[].


Your example code does...

  • not compile for a variety of reasons, typos, and omissions; and

  • does not contain any obvious error that would explain the behaviour you observed.

It is also being nonsensical (locations being absent, for example). That means that either you're seeing ghosts, or your example code doesn't show what the real code really does (and does no longer contain the bug in question because you cut it away).

Either way, nobody could really tell because you did not give enough information.

Try this little debugging technique.

Edit: After you updated your question, at least your example code compiles properly (after I added the typedef). It accesses undefined memory on the first loop (because an array defined as e.g. options[3] does have elements options[0] .. options[2], but not options[3]). What does not happen is that the value of loc_ptr changes in any way (as I verified using a debugger). So either you're still seeing ghosts, or your example code does not exhibit the problem you are having (making it worthless as an example). My recommendation (see above link) remains.


Form the code, it seems that the first call in on the not existing element locations[3]. Is that significant?


The issue seems to be at

loc_ptr = &locations[0];

locations seems to be an array of location elements. Getting the address of the first element of this array will give you a pointer to this element.

Why not just do locations[position] instead of creating the extra loc_ptr to hold the same information? I'm pretty sure the issue is with the content of locations - which is also not in the scope of your code snippet.


Your can't access location[3] since the highest index in that array is 2. You probably wanted:

for (int i = 2;i >= 0;i--) 
    options[i] = calculate_distance(current_location, locations[i]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜