类数组内部的C++深拷贝const char *?

标签 c++ arrays string deep-copy

所以我在上课时遇到问题。在这个类中,我应该有一些数据数组。我对分配和创建新数据没有问题,例如 x2.NewAccount("123456", 1000);这很好用,问题是当我尝试使用针对某个变量的字符串创建数据时。我对深度复制有所了解,但我不知道如何在我的情况下编程 = 运算符 + 我认为 strcpy 但这也不起作用。

PS:它是一个学校项目,所以请不要因为我没有使用标题和使用一堆我没有在代码中使用的包含而评判我。它由我的学校制作,我不允许更改它们+添加它们(我知道使用 c++ 中的字符串会容易得多。)。
谢谢你的帮助。

#ifndef __PROGTEST__
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <cctype>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
#endif /* __PROGTEST__ */

struct data_history{
    int money = 0;
    bool Income;
    const char * UnStr;
    const char * to_from;
};

struct client{
    const char * accID;
    int Balance;
    int def_bal;
    data_history * history;
    int in_index = 0;
    int in_cap = 10;

    friend ostream &operator << (ostream &output , client p){
        output << p.accID << ":" << endl << "   " << p.def_bal << endl;
        for (int i = 0 ; i < p.in_index ; i++){
            if (p.history[i].Income == false)
                output << " - " << abs(p.history[i].money) << ", to: " << p.history[i].to_from << ", sign: " << p.history[i].UnStr << endl;
            else
                output << " + " <<abs(p.history[i].money) << ", from: " << p.history[i].to_from << ", sign: " << p.history[i].UnStr << endl;
        }
        output << " = " << p.Balance << endl;
        return output;
    }
};

class CBank
{
public:
    int cap = 10;
    int index = 0;
    client * database;
    ~CBank(){
        for (int i = 0 ; i < index ; i++)
                delete[] database[i].history;
        delete[]database;
    }
    CBank(){
        database = new client[cap];
    }
    bool NewAccount ( const char * accID, int initialBalance ){
        for(int i = 0 ; i < index ; i ++)
            if (accID == database[i].accID) {
                return false;
            }
        //strcpy (database[index].accID , accID );  // Im getting errors while compileing (cuz I was using const char * for database.accID when i chenged it i got program crash. 
        database[index].accID = accID;
        database[index].Balance = initialBalance;
        database[index].def_bal = initialBalance;
        database[index].in_cap = 10;
        database[index].history = new data_history[database[index].in_cap];
        index ++;
        return true;
    }
    client Account (const char * accID ){
        const char * input  =accID;
        for (int i = 0 ; i < index ; i++){
            if (database[i].accID == input )
                return database[i];
        }
        throw "error";
    }
    void print (){
        for (int i = 0 ; i  < index ; i ++) {
            cout << endl;
            cout << i << " = "<< " ID = " << database[i].accID << " | Balance = " << database[i].Balance << endl;
            cout << "===Account history ===\n\n";
            for (int y = 0 ; y < database[i].in_index; y++) {
                cout << "Was it for him? : " << boolalpha << database[i].history[y].Income
                    << "\nHow much : " << database[i].history[y].money << "\nUnique string : "
                    << database[i].history[y].UnStr << "\nfrom/to: " << database[i].history[y].to_from << endl << endl;
            }
        }
    }

private:

};


#ifndef __PROGTEST__
int main ( void )
{
    char accCpy[100], debCpy[100], credCpy[100], signCpy[100];
    CBank x2;
    strncpy ( accCpy, "123456", sizeof ( accCpy ) );
    assert ( x2 . NewAccount ( accCpy, 1000 ) );
    x2 . print();
    cout << "\n\n\n\n";
    strncpy ( accCpy, "987654", sizeof ( accCpy ) );
    assert ( x2 . NewAccount ( "987654", -500 ) );
    x2 . print();
}
#endif /* __PROGTEST__ */

最佳答案

使用 database[index].accID = accID; 时你只是在做一个浅拷贝(并且依靠调用者来保持这个内存有效,因为指针可能被访问)。

您已正确确定需要执行深层复制,但是 client::accID只是一个指针,但在初始化它以指向某个内存之前,您可能不会复制到它。

一种方法是动态分配和管理 client::accID类似于您如何动态分配和管理 client::history .

而不是 strcpy (database[index].accID , accID );database[index].accID = accID; , 尝试:

size_t bufsize = strlen(accID) + 1;
char *buf = new char[bufsize];
memcpy(buf, accID, bufsize);
database[index].accID = buf;

并在析构函数中添加:
delete[] database[i].accID

正如其他人所指出的,这种 C++ 编程风格非常容易出错,并且社区并不看好。使用标准库类可以轻松避免这种手动内存管理。即使进行了上述更改,如果您开始复制 CBank,您的程序也会遇到未定义的行为。对象。

即使您不必为您的作业这样做,您也应该考虑尝试将其重写为练习:
  • std::string而不是 accID; 的 C 字符串
  • std::vector而不是 data_history数组


  • 也仅供引用:
    if (database[i].accID == input )
    

    不会用 c-strings 做期望的事情......

    关于类数组内部的C++深拷贝const char *?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61066327/

    相关文章:

    c++ - 了解 PNG 文件格式 IDAT 段

    c++ - Linux 中的 OpenCv VideoCapture 错误 - 不支持设置属性 #0

    c++ - 标准保证初始化顺序吗?

    c - sscanf 到结构数组

    java - 对不同子数组中数组内的重复项进行排序

    c - 将文件读取到数组字符串

    c - 在结构中使用字符串

    c++ - 模板格式化下的结构常量错误

    arrays - 如何将字符串转换为byte() vb.net?

    检查元音的 JavaScript 函数