【问题标题】:Dynamic Memory Allocation and using Classes动态内存分配和使用类
【发布时间】:2014-10-03 13:58:31
【问题描述】:

我正在尝试实现一个 GradeManager 类,该类在内部使用使用 new 运算符动态创建的 DataVector 对象数组来记录一组学生的作业成绩。

我正在努力制作构造函数/析构函数。

构造函数的说明:“这是班级的唯一构造函数,它指定班级的学生nStudents和作业nHW的数量。您应该使用它们来动态设置数组大小。”

您提供的任何想法都会有很大帮助!这就是我到目前为止所拥有的。非常感谢!!!

#include <iostream>
#include <cmath>
#include <iomanip>
//DO NOT INCLUDE ANYTHING ELSE!!
using namespace std;

typedef double DataType;//Alias for double type
typedef unsigned int UIntType;//Alias for unsigned int type

class DataVector
{
private:
    DataType *m_data;           //Pointer to dynamically allocated memory that holds all items
    UIntType m_size;            //Size of the m_data array

public:

    DataVector()
    {
        m_data = new DataType[10];

        for(int i = 0; i < 10; i++){
            m_data[i]=0;
        }

        m_size = 10;

    }

    DataVector(UIntType initSize, DataType initValue)

    {
        int arraySize = initSize;
        m_data = new DataType[arraySize];

        for(int i = 0; i < arraySize; i++){
            m_data[i] = initValue;
        }
        m_size = initSize;
    }

    ~DataVector()
    {
        delete [] m_data;
        m_data = NULL;
    }

    UIntType GetSize()
    {
        return m_size;
    }

    void Reserve(UIntType newSize)

    {
        int arraySize = newSize;

        DataType *new_data;
        new_data = new DataType[arraySize];

        for(int i = 0; i < m_size; i++){
            new_data[i] = m_data[i];}

        m_data = new_data;
        m_size = newSize;
    }

};

class GradeManager
{
private:
    DataVector *m_student;//m_student[0], m_student[1], etc correspond to sID 0, 1, etc respectively
    UIntType m_nStudents;//Number of students

public:
    GradeManager(UIntType nStudents, UIntType nHWs)
    {
        m_student = new DataVector[nStudents];

        m_student->Reserve(nHWs);

        m_nStudents = nStudents;

    }

    ~GradeManager()
    {

        int numOfStudents = m_nStudents;

        for(int i = 0; i < numOfStudents; i++)
            delete [] m_student;                   

        m_student = NULL;
    }

};

【问题讨论】:

  • 第一个想法:使用std::vector&lt;&gt;
  • 尽可能避免使用new(仅在确实需要时使用),因为@quantdev 建议使用std::vector
  • 因此您不能包含任何其他内容,但您使用NULL,不能保证在任何现有标题中。
  • 评论//DO NOT INCLUDE ANYTHING ELSE!! 是否意味着这是家庭作业,您现在可以使用标准容器?

标签: c++ class dynamic-memory-allocation


【解决方案1】:

一些想法:

  1. 如 quantdev 评论中所述,在 DataVector 类中使用 std::vector - 比使用数组简单得多,但您不能直接限制其大小(使用数组几乎可以自动完成)。
  2. DataVector::Reserve() 中似乎存在内存泄漏和分段错误。您为 new_data 分配新内存,将数据从 m_data 复制到 new_data (这让我想到了一个想法;如果 m_size 大于 newSize 会发生什么?IMO,内存访问错误,但我不确定),然后重新将 m_data 指向 new_data,而不会在先前的调用(例如,在构造函数中)释放存储在 m_data 中的数据。这会导致内存泄漏。
  3. 另外,我不完全确定 DataVector::Reserve() 方法是否保留任何空间,如果这是 它的目的是什么。

此外,在我看来,GradeManager 的构造函数/析构函数看起来还不错,大多数问题实际上位于 DataVector 类中。

祝你好运!

【讨论】:

    【解决方案2】:

    我假设您不允许使用标准容器,例如 std::vector 或 std::array 这可以促进这项工作。

    您的 DataVector 构造函数和析构函数是一致的:您创建一个新的动态数组并删除该动态数组。

    然而,在这两者之间,您在 GradeMaster 构造函数中调用了函数 Reserve()。 它的for 循环可能会超出范围,因为新的大小可以比旧的更大或更小。您必须检查 i 是否在源和目标的范围内:

    for (int i = 0; i < m_size && i<arraySize; i++){   //  check on source and target bounds !!! 
    

    此外,不释放不再需要的旧对象也会造成内存泄漏。您必须在循环结束后插入这一行:

    delete[] m_data; //  insert this to avoid memory leaks 
    

    最后一点是在 GradeMaster 析构函数中。当您使用delete[] 删除整个数组时,您不得循环并尝试多次删除数组!删除数组,将删除其所有元素。只需删除 for 行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 2016-05-05
      • 1970-01-01
      • 2012-01-13
      • 2020-04-21
      • 2020-04-30
      相关资源
      最近更新 更多