无法在C ++中分配内存

时间:2017-12-16 07:32:42

标签: c++

大家好我无法使用new运算符在c ++中分配内存。代码如下

#include <iostream>
#include<stack>
#include<queue>
#include<string>

using namespace std;

class cell
{
    public:
    unsigned short int x, y;
    unsigned short int dis;
    cell* parent;
    string step;
    cell(){}
    cell(unsigned short int x, unsigned short int y, cell* parent, unsigned short int d, string s):x(x), y(y), parent(parent), dis(d), step(s){}
};

bool isinside(int i, int j, int n)
{
    if(i >= 0 && i < n && j >= 0 && j < n)
        return true;
    return false;
}

void printShortestPath(unsigned short int n, unsigned short int i_start, unsigned short int j_start, unsigned short int i_end, unsigned short int j_end)
{
    //  Print the distance along with the sequence of moves.
   // cout << "safe1.1";
    queue<cell*> q;
    bool** vis = new bool*[n+1];
    cout << "safe1.2\n";
    for(unsigned short int i = 0; i < n+1; i++)
        *(vis+i) = new bool;
    cout << "safe1.3\n";
    for(unsigned short int i = 0; i < n; i++)
        for(unsigned short int j = 0; j < n; j++)
            vis[i][j] = false;
    cout << "safe1.5\n";
    cell* c;
    q.push(c = new cell(i_start, j_start, NULL, 0, "0"));   //**ERROR HERE**
    cout << "safe1.4\n";
    bool reached = false;
    cell* t;
    cout << "safe2\n";
    while(!q.empty())
    {
        t = q.front();
       // cout << t->x << " " << t->y << " " << t->step << " " << t << endl;
        q.pop();
        vis[t->x][t->y] = true;
        cout << t << " ";
        if(t->x == i_end && t->y == j_end)
        {
            reached = true;
            break;
        }
        if(isinside(t->x-2,t->y-1,n) && !vis[t->x-2][t->y-1])
        {
            q.push(new cell(t->x-2,t->y-1,t,t->dis+1, "UL"));
            vis[t->x-2][t->y-1] = true;
        }
        if(isinside(t->x-2,t->y+1,n) && !vis[t->x-2][t->y+1])
        {
            q.push(new cell(t->x-2,t->y+1,t,t->dis+1, "UR"));
            vis[t->x-2][t->y+1] = true;
        }
        if(isinside(t->x,t->y+2,n) && !vis[t->x][t->y+2])
        {
            q.push(new cell(t->x,t->y+2,t,t->dis+1,"R"));
            vis[t->x][t->y+2] = true;
        }
        if(isinside(t->x+2,t->y+1,n) && !vis[t->x+2][t->y+1])
        {
            q.push(new cell(t->x+2,t->y+1,t,t->dis+1,"LR"));
            vis[t->x+2][t->y+1] = true;
        }
        if(isinside(t->x+2,t->y-1,n) && !vis[t->x+2][t->y-1])
        {
            q.push(new cell(t->x+2,t->y-1,t,t->dis+1,"LL"));
            vis[t->x+2][t->y-1] = true;
        }
        if(isinside(t->x,t->y-2,n) && !vis[t->x][t->y-2])
        {
            q.push(new cell(t->x,t->y-2,t,t->dis+1,"L"));
            vis[t->x][t->y-2] = true;
        }
        cout << t << endl;
    }
    cell* root = t;
    if(reached)
    {
        cout << "safe3\n";
        stack<string> st;
        unsigned short int aa = 0;
        while(root != NULL)
        {
            aa++;
            st.push(root->step);
         //   cout << "root " << root << endl;
            root = root->parent;
        }
        cout << aa-1 << endl;
        st.pop();
        while(!st.empty())
        {
            cout << st.top() << " ";
            st.pop();
        }

    }
    else
    {
        std::cout << "Impossible"; // If I add return before this it does not give error in some cases.
    }
    cell* r = root;
    while(root)
    {
        root = root->parent;
        delete r;
        r = root;
    }
    return;
}

int main() {
    try
    {
    int n;
    cin >> n;
    unsigned short int i_start;
    unsigned short int j_start;
    unsigned short int i_end;
    unsigned short int j_end;
    cin >> i_start >> j_start >> i_end >> j_end;
    cout << "safe1\n";
    printShortestPath(n, i_start, j_start, i_end, j_end);
    }
    catch(...)
    {
        cout << "Not enough memory";
    }
    return 0;
}
  

当我提供大输入示例时     100个
    0 0 0 19   
输出是   
safe1
safe1.2
safe1.3
safe1.5

,显示的错误是

solution: malloc.c:2394: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.

GDB trace:
Reading symbols from solution...done.
[New LWP 14031]
Core was generated by `solution'.
Program terminated with signal SIGABRT, Aborted.
#0  0x00007f92ff084428 in __GI_raise (sig=sig@entry=6)
    at ../sysdeps/unix/sysv/linux/raise.c:54
#0  0x00007f92ff084428 in __GI_raise (sig=sig@entry=6)
    at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007f92ff08602a in __GI_abort () at abort.c:89
#2  0x00007f92ff0cc2e8 in __malloc_assert (
    assertion=assertion@entry=0x7f92ff1e0150 "(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)", 
    file=file@entry=0x7f92ff1dcb85 "malloc.c", line=line@entry=2394, 
    function=function@entry=0x7f92ff1e0998 <__func__.11509> "sysmalloc")
    at malloc.c:301
#3  0x00007f92ff0d0426 in sysmalloc (nb=nb@entry=64, 
    av=av@entry=0x7f92ff413b20 <main_arena>) at malloc.c:2391
#4  0x00007f92ff0d1743 in _int_malloc (
    av=av@entry=0x7f92ff413b20 <main_arena>, bytes=bytes@entry=48)
    at malloc.c:3827
#5  0x00007f92ff0d3184 in __GI___libc_malloc (bytes=48) at malloc.c:2913
#6  0x00007f92ff6c0498 in operator new(unsigned long) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x0000000000401499 in printShortestPath (n=100, i_start=<optimized out>, 
    j_start=<optimized out>, i_end=0, j_end=19)
    at solution.cc:38
#8  0x0000000000401018 in main () at solution.cc:135

从输出中可以清楚地看到“safe1.4”没有打印出来,所以错误似乎与评论“ERROR HERE”一致
请帮助。

1 个答案:

答案 0 :(得分:1)

我没有详细阅读所有代码,但行:

for(unsigned short int i = 0; i < n+1; i++) *(vis+i) = new bool;

看起来很可疑。如果这是我认为的那样,那就是尝试将bool指针分配给大小为n + 1的bool指针数组中的每个元素。

但是,向指针添加int很可能无法提供预期的结果。见http://www.cplusplus.com/doc/tutorial/pointers/

可能会尝试:

bool* cur_vis_ptr =*vis;
for(unsigned short int i = 0; i < n+1; i++){
   cur_vis_ptr=new bool;
   cur_vis_ptr++;
}

或许更好的简单方法:

for(unsigned short int i = 0; i < n+1; i++)
   vis[i]=new bool;