gpt4 book ai didi

c++ - 段错误 : 11 and malloc errors in C++ code

转载 作者:行者123 更新时间:2023-11-28 08:09:05 25 4
gpt4 key购买 nike

好的,所以我知道这段代码中可能有很多错误。我对动态内存分配、指针等还很陌生。

头文件 account.h 是由我们的教授提供给我们的。我们被告知不要对 .h 文件进行任何更改。

实现文件是我写的。主要功能仅用于基本的初始测试。我们得到了另一个文件来实际测试帐户类的实现。

如果我不注释掉 cout 名称行,我会收到 seg fault 11 错误。如果我这样做,它会打印帐号,但会抛出此错误:

测试(29976)malloc:* 对象 0x62c1aa18c9d8374 错误:未分配正在释放的指针* 在 malloc_error_break 中设置断点进行调试
中止陷阱:6

任何帮助都将不胜感激!

这是头文件:

class account
{
public:
typedef char* string;
static const size_t MAX_NAME_SIZE = 15;
// CONSTRUCTOR
account (char* i_name, size_t i_acnum, size_t i_hsize);
account (const account& ac);
// DESTRUCTOR
~account ( );
// MODIFICATION MEMBER FUNCTIONS
void set_name(char* new_name);
void set_account_number(size_t new_acnum);
void set_balance(double new_balance);
void add_history(char* new_history);
// CONSTANT MEMBER FUNCTIONS
char* get_name ( ) const;
size_t get_account_number ( ) const;
double get_balance( ) const;
size_t get_max_history_size( ) const;
size_t get_current_history_size ( ) const;
string* get_history( ) const;
friend ostream& operator <<(ostream& outs, const account& target);
private:
char name[MAX_NAME_SIZE+1]; //name of the account holder
size_t ac_number; //account number
double balance; //current account balance
string *history; //Array to store history of transactions
size_t history_size; //Maximum size of transaction history
size_t history_count; //Current size of transaction history
};

这里是实现文件:

// File: account.cxx
// Author: Mike Travis
// Last Modified: Mar 3, 2012
// Description: implementation of Account class as prescribed by the file account.h

#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "account.h"

using namespace std;

//Constructor

account::account(char* i_name, size_t i_acnum, size_t i_hsize){
string *d_history;
d_history = new string[i_hsize];

for(int i = 0; i<i_hsize; i++){
name[i] = i_name[i];
}
ac_number = i_acnum;
history_size = i_hsize;
history_count = 0;
}


account::account(const account& ac){
string *d_history;
d_history = new string[ac.history_size];

for( int i=0; i<ac.get_current_history_size(); i++){
strcpy(d_history[i], history[i]);
}

strcpy(name,ac.get_name());
ac_number = ac.get_account_number();
history_size = ac.get_max_history_size();
history_count = ac.get_current_history_size();
}

account::~account(){ delete [] history; }

void account::set_name(char* new_name){ strcpy(name, new_name); }
void account::set_account_number(size_t new_acnum){ ac_number = new_acnum; }
void account::set_balance(double new_balance){ balance = new_balance; }
void account::add_history(char* new_history){
strcpy(history[history_count], new_history);
history_count++;
}

char* account::get_name() const {
char* name_cpy;
strcpy(name_cpy, name);
return name_cpy;
}

size_t account::get_account_number() const{ return ac_number; }
double account::get_balance() const{ return balance; }
size_t account::get_max_history_size() const{ return history_size; }
size_t account::get_current_history_size() const{ return history_count; }
//string* account::get_history() const{ return *history; }


int main(){

account test1("mike travis", 12345, 20);
//cout<<"\nname: "<< test1.get_name();

cout<<"\n\nacnum: "<<test1.get_account_number()<<"\n\n";

return 0;
}

最佳答案

account 的析构函数中,您删除了history 数组。但是,在构造函数中,您分配(并泄漏)了一个存储在局部变量 d_history 中的数组。您可能想将其分配给成员变量 history 而不是 - 因为您没有,如果您进入析构函数,它会给您一个错误,提示您正在释放 history 但从未分配过它。

复制构造函数也有类似的错误。

您的代码中还有其他错误,我假设您会在执行过程中发现这些错误 - 例如,get_name() 将不起作用。我怀疑头文件在这里没有帮助,但如果您不应该更改它,也没有什么可做的。

关于c++ - 段错误 : 11 and malloc errors in C++ code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com