开发者

C++ pair delete error [closed]

开发者 https://www.devze.com 2023-02-23 03:34 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

*Update: * Finally I find the problem. The problem is like this:

pair<int,int> *findPair(){
    pair<int,int> *t = new pair<int,int> [10];
    for(int i=0;i<11;i++) // error here
    {
        t[i].first =1;
        t[i].second =2;
    }
    return t;
}

int main() {
    pair&开发者_C百科lt;int, int> * tt = findPair();
    tt[1].first =8;
    delete [] tt; // error
    return 0;
}

There is segment fault error. gcc does not show any warning for out of bounds error. In my original code, I use k as index of the pair array. k changes at each while loop and ends up with a value out of bounds. Thanks you all ans sorry for any inconvenience caused.


I copied your code into a .cc file (with the right #include), and this code works just fine. Are you sure this is where the segmentation fault is at?

If you are using linux, and have no core file, use "ulimit -c unlimited". Then you can load the core file in gdb to check where the error takes place.

0

精彩评论

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