Sun c ++编译器的分段错误

Fahad Ur Rehman

我在AccountMap.h和AccountMap.cpp中有一个AccountMap类。它从二进制文件中读取内容(可以)并输出内容。它在g ++编译器上运行得非常好,但是当我在函数之一中使用new运算符进行10次迭代时,却在sun c ++ CC编译器上出现了段错误。如果我忽略段错误,它仍然会输出正确的内容。这是我的AccountMap.h:

#ifndef account_map
#define account_map

#include <iostream>
#include <map>
#include <cstring>
#include "Account.h"

using namespace std;

struct CompareCharArrays
{
    bool operator()(const char* a, const char* b) const //overloading () operator 
    {
        if((strcmp(a,b) < 0))
        {
            return true;
        }
        return false;
    }
};

class AccountMap
{
    public:
        ~AccountMap(); //destructor
        void loadData(const char *); //loads data from a  given file
        int countRecs(const char *); //counts total number of records in the given file
        void displayData(); //displays data


    private:
        multimap<const char *, Account, CompareCharArrays> accounts; //a multipmap
        Account** account_manager; //two-D array of accounts
        char** key_manager; //two-D array of keys
        int total_accounts; //total number of accounts
};

#endif

这是我的AccountMap.cpp函数,导致seg错误:

void AccountMap::loadData(const char * file) //loads data from a  given file
{
    int num = countRecs(file); //total no of records in the file
    ifstream in(file, ios::in | ios::binary);
    if(!in)
    {
        cerr<<"File doesn't exist!"<<endl;
        exit(1);
    }
    this->account_manager = new (nothrow) Account*[num]; 
    if(this->account_manager == NULL)
    {
        exit(1);
    }
    this->key_manager = new (nothrow) char*[num]; //seg fault here during 10th iteration
    if(this->key_manager == NULL)
    {
        exit(1);
    }
    simplyfy_data acc;
    this->total_accounts = 0;
    while(in.read((char*)&acc, sizeof(simplyfy_data)))
    {
        this->account_manager[this->total_accounts] = new (nothrow) Account;
        if(this->account_manager[this->total_accounts] == NULL)
        {
            exit(1);
        }
        this->key_manager[this->total_accounts] = new (nothrow) char;
        if(this->key_manager[this->total_accounts] == NULL)
        {
            exit(1);
        }
        string date;
        int len = strlen(acc.dob);
        for(int i = 0; i < len; i++)
        {
            date.push_back(acc.dob[i]);
        }
        account_manager[this->total_accounts]->set_data(acc.number,acc.name,acc.sex,date,acc.address,acc.balance);
        strcpy(key_manager[this->total_accounts],acc.name);
        this->accounts.insert(pair<const char* const, Account>(key_manager[this->total_accounts],(*account_manager[this->total_accounts]))); //inserting
        this->total_accounts++;
    }
    in.close();
}

错误是:

signal SEGV (no mapping at the fault address) in realfree at 0xa2958440
0xa2958440: realfree+0x0068:    ld       [%o7 + 8], %o5
托尼·德罗伊(Tony Delroy)

您为一个字符分配空间...

this->key_manager[this->total_accounts] = new (nothrow) char;

然后复制很多

strcpy(key_manager[this->total_accounts],acc.name);

你应该用std::stringstd::vector,并避免newconst char*multimap键:你会大规模更有可能避免的错误(包括内存泄漏,特别是如果你有例外)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C中的分段错误编译器错误

来自分类Dev

C编译器错误或程序错误?

来自分类Dev

C,编译器错误和指针

来自分类Dev

C#-编译器错误

来自分类Dev

c编译器,重叠内存错误

来自分类Dev

那是C ++编译器错误吗?

来自分类Dev

编译器错误C2955

来自分类Dev

编译器错误-“错误:找不到或加载主类com.sun.tools.javac.Main”

来自分类Dev

在某些C ++编译器上使用限制限定符的编译器错误

来自分类Dev

C ++中的奇数编译器错误(VC编译器)

来自分类Dev

Cmake错误:未知的CXX编译器标识,未知的C编译器标识

来自分类Dev

在线编译器中的分段 C++ 没有打印消息

来自分类Dev

C ++标头#define编译器错误

来自分类Dev

TUN / TAP上的C编译器错误

来自分类Dev

C#mono的奇怪编译器错误

来自分类Dev

C ++程序编译器错误:无匹配功能

来自分类Dev

llvmgen.c中的xmos内部编译器错误?

来自分类Dev

C ++ 11编译器的构造函数继承生成错误

来自分类Dev

C和DLC编译器的奇怪解析错误

来自分类Dev

C ++编译器错误多个变量定义

来自分类Dev

使用typeof(Expression <Func <,>>)的C#编译器错误

来自分类Dev

局部模板指定的编译器错误(C ++)

来自分类Dev

C ++编译器选择了错误的重载函数

来自分类Dev

简单C链表中的奇怪编译器错误

来自分类Dev

OpenCL编译器错误C4996

来自分类Dev

C和DLC编译器的奇怪解析错误

来自分类Dev

通用类中的编译器错误c1001

来自分类Dev

C ++:来自DLL的编译器错误覆盖功能

来自分类Dev

C ++缺少编译器错误信息