【问题标题】:How to Delete Dynamic Memory allocated in Class Constructor using destructor如何使用析构函数删除在类构造函数中分配的动态内存
【发布时间】:2021-07-16 05:26:32
【问题描述】:

我正在尝试使用析构函数释放对象的内存。该内存是在对象声明时分配的。下面的代码 sn-p 是我正在制作的程序的综合版本,但它解释了我的问题。 我知道问题是通过析构函数释放的,但我不知道可能导致异常的原因。

#include <iostream>
using namespace std;
class student 
{
private:
    char const* firstname;
    char const* lastname;
public:
    student()
    {
        firstname = new char[10];
        lastname = new char[10];
        firstname = "HelloMaria";
        lastname = "Something";
    }
    void print()
    {
        cout << "Firstname =  " << firstname<<endl;
        cout << "Last name = " << lastname << endl;
    }
    ~student()
    {
        cout << "Destructor Called" << endl;
        delete[] firstname;
        delete[] lastname;
    }
};
int main()
{
    student obj1;
    obj1.print();
    system("pause");
    return 0;

}

【问题讨论】:

  • 真正的问题是什么?
  • 请注意,在学生班级的默认构造函数中,lastname="Something" 我错误地将其写为“SomethingVe”,即使它是Something而不是SomethingVe,异常问题仍然存在。
  • firstname = "HelloMaria";firstname 更改为指向字符串文字而不是您分配的数组。您正在分配 char 数组,但随后会覆盖指向它们的指针……您会泄漏内存并且以后无法释放它们。此外,它们无论如何都不够大(您需要足够的空间来终止 null)。
  • firstname = new char[10]; 分配 10 字节的内存。 firstname = "HelloMaria"; 覆盖该指针并泄漏您刚刚分配的内存。当析构函数被调用时,你尝试使用delete 从未使用new 分配的内存。使用strcpy 将字符串复制到分配的内存中,并注意为所有字符分配足够的空间,并为终止的0 额外分配一个空间。对于“HelloMaria”,10 是不够的,它必须至少为 11。
  • @RetiredNinja 也必须更改指针。 strcpy 不会喜欢 char const* 目标,显然 OP 将其推入其中以压制将文字地址保存给这些成员的警告。

标签: c++ c++11 visual-c++


【解决方案1】:

您的问题出在以下几行:

    firstname = new char[10];
    lastname = new char[10];
    firstname = "HelloMaria";
    lastname = "SomethingVe";

首先,您将firstnamelastname 都设置为指向新分配的char 数组,这很好……但随后您立即将它们更改为指向静态分配的字符串。这意味着当您在析构函数中传递指向 delete[] 的指针时,您正在尝试删除您从未分配过的字符串,这会调用未定义的行为。

处理字符串的正确方法是使用std::string,而不是分配您自己的字符缓冲区,但如果由于某种原因您不能这样做,那么下一个最佳方法就是执行您正在尝试的操作做会更像这样:

student()
{
    firstname = alloc_string_buffer("HelloMaria");
    lastname = alloc_string_buffer("SomethingVe");
}

private:
   // Helper function to allocate the right-sized char-buffer and 
   // copy the bytes from the passed-in literal string into the buffer
   char * alloc_string_buffer(const char * s)
   {
       size_t numBytesToAllocate = strlen(s)+1;  // +1 for the NUL terminator byte
       char * buf = new char[numBytesToAllocate];
       strcpy(buf, s);
       return buf;
   }

【讨论】:

    猜你喜欢
    • 2017-08-17
    • 1970-01-01
    • 2015-12-31
    • 2017-07-06
    • 2013-03-06
    • 1970-01-01
    • 2016-06-14
    • 2021-11-29
    • 2020-05-25
    相关资源
    最近更新 更多