核心转储,同时动态分配内存

时间:2016-09-02 15:19:21

标签: c++

我想创建一个具有以下定义的Matrix类

#include <iostream>
#include <vector>

using namespace std;

void display(vector<int>arr);
int get(void);
vector<int> radix_sort(vector<int>);

class Input{
private:
    int temp;
public:
    int get(){
        cin >> temp;
        return temp;
    }
};

class Matrix {
private:
    int **matrix;
public:
    Matrix(int r,int c){
        this->matrix = new int*[r];
        for(int i = 0;i < r; i++)
            this->matrix[i] = new int[c];
    }
    int get(int r,int c){
        return this->matrix[r][c];
    }
};
int main() {
    Input inp;

    int n;
    cout<<" Enter Number of Elements : ";
    cin >> n;

    vector<int> arr;
    cout <<"Enter "<<n<<" elements\n";
    for(int i = 0 ; i < n ; i++)
        arr.insert(arr.begin() + i,inp.get());
    cout <<"Before Sort\n";
    display(arr);
    radix_sort(arr);

}
void display(vector<int> arr){
    for(int i = 0 ;i < arr.size(); i++)
        cout << arr.at(i) << " ";
    cout<<endl;
}

vector<int> radix_sort(vector<int> arr){
    Matrix mat(arr.size(),10); // This call is creating the problem.
}

此代码应创建一个类似于数据结构的矩阵,其中包含arr.size()行和10列。但是在运行时我面临着与很多内存相关信息的coredump。

Error in `/home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux  /radix_sort': free(): invalid pointer: 0x0000000000bb0468
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7f5e411b6725]
/lib/x86_64-linux-gnu/libc.so.6(+0x7ff4a)[0x7f5e411bef4a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f5e411c2abc]
/home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort[0x40239c]
/home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort[0x402094]
/home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort[0x401a16]
/home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort[0x401573]
/home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort[0x40126b]
/home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort[0x400f6d]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f5e4115f830]
/home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort[0x400d19]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:06 11011497                          /home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort
00603000-00604000 r--p 00003000 08:06 11011497                               /home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort
00604000-00605000 rw-p 00004000 08:06 11011497                           /home/parnab/NetBeansProjects/Radix Sort/dist/Debug/GNU-Linux/radix_sort
00b9e000-00bd0000 rw-p 00000000 00:00 0                                  [heap]
7f5e3c000000-7f5e3c021000 rw-p 00000000 00:00 0 
7f5e3c021000-7f5e40000000 ---p 00000000 00:00 0 
7f5e40e36000-7f5e40f3e000 r-xp 00000000 08:06 528886                     /lib/x86_64-linux-gnu/libm-2.23.so
7f5e40f3e000-7f5e4113d000 ---p 00108000 08:06 528886                     /lib/x86_64-linux-gnu/libm-2.23.so
7f5e4113d000-7f5e4113e000 r--p 00107000 08:06 528886                     /lib/x86_64-linux-gnu/libm-2.23.so
7f5e4113e000-7f5e4113f000 rw-p 00108000 08:06 528886                     /lib/x86_64-linux-gnu/libm-2.23.so
7f5e4113f000-7f5e412ff000 r-xp 00000000 08:06 528816                     /lib/x86_64-linux-gnu/libc-2.23.so
7f5e412ff000-7f5e414fe000 ---p 001c0000 08:06 528816                     /lib/x86_64-linux-gnu/libc-2.23.so
7f5e414fe000-7f5e41502000 r--p 001bf000 08:06 528816                     /lib/x86_64-linux-gnu/libc-2.23.so
7f5e41502000-7f5e41504000 rw-p 001c3000 08:06 528816                     /lib/x86_64-linux-gnu/libc-2.23.so
7f5e41504000-7f5e41508000 rw-p 00000000 00:00 0 
7f5e41508000-7f5e4151e000 r-xp 00000000 08:06 524378                      /lib/x86_64-linux-gnu/libgcc_s.so.1
7f5e4151e000-7f5e4171d000 ---p 00016000 08:06 524378                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f5e4171d000-7f5e4171e000 r--p 00015000 08:06 524378                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f5e4171e000-7f5e4171f000 rw-p 00016000 08:06 524378                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f5e4171f000-7f5e41891000 r-xp 00000000 08:06 3672061                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f5e41891000-7f5e41a91000 ---p 00172000 08:06 3672061                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f5e41a91000-7f5e41a9b000 r--p 00172000 08:06 3672061                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f5e41a9b000-7f5e41a9d000 rw-p 0017c000 08:06 3672061                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f5e41a9d000-7f5e41aa1000 rw-p 00000000 00:00 0 
7f5e41aa1000-7f5e41ac7000 r-xp 00000000 08:06 528788                     /lib/x86_64-linux-gnu/ld-2.23.so
7f5e41c9e000-7f5e41ca3000 rw-p 00000000 00:00 0 
7f5e41cc3000-7f5e41cc6000 rw-p 00000000 00:00 0 
7f5e41cc6000-7f5e41cc7000 r--p 00025000 08:06 528788                     /lib/x86_64-linux-gnu/ld-2.23.so
7f5e41cc7000-7f5e41cc8000 rw-p 00026000 08:06 528788                     /lib/x86_64-linux-gnu/ld-2.23.so
7f5e41cc8000-7f5e41cc9000 rw-p 00000000 00:00 0 
7ffe051b3000-7ffe051d4000 rw-p 00000000 00:00 0                          [stack]
7ffe051ec000-7ffe051ee000 r--p 00000000 00:00 0                          [vvar]
7ffe051ee000-7ffe051f0000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
0
RUN FINISHED; Aborted; core dumped; real time: 2s; user: 0ms; system: 0ms

我的代码出了什么问题?请帮助。

1 个答案:

答案 0 :(得分:3)

vector<int> radix_sort(vector<int> arr){
    Matrix mat(arr.size(),10); // This call is creating the problem.
    return something???; // <--- missing
}

你的函数必须按值返回vector<int>,但你不返回任何东西,所以垃圾返回。您应该将编译器配置为在这种情况下(或至少警告)产生错误