【问题标题】:c++ dynamically allocated resizable arrayc++动态分配可调整大小数组
【发布时间】:2020-11-26 07:27:34
【问题描述】:

好的,所以基本上是的,我的作业需要帮助,我什至不会说谎,哈哈^^;所以我们要做的一件事就是让所有这些方法和谐地协同工作,直到我们实验室所在的编译器程序说通过image of complier showing the pass or fail messages

这是编译器的另一张图片,以及它在运行时如何根据结果显示通过或失败

other image of the complier showing the pass or fail of the result of the functions entered as it communicates to the ..thing

所以我有以下需要帮助的事情,所以默认设置,因为它显示通过,我相信我做对了,还有其他一些事情我仍然需要帮助,是的,我自己一直在努力我只是很好的指针总是把我搞砸了xD提前谢谢。

template<typename Type>
class DynArray {

    friend class TestSuite; // Giving access to test code

    Type* mArray;
    unsigned int mSize;
    unsigned int mCapacity;

public:

    // Default constructor
    //      Creates a new object
    // In:  _startingCap        An initial capacity  to start the array at (optional)
    DynArray(unsigned int _startingCap = 0)
    {
    
        
        mArray = new Array[strlen(_startingCap) + 1];
        strcpy_s(mArray, strlen(_startingCap) + 1, _startingCap);

    }
    

    // Destructor
    //      Cleans up all dynamically allocated memory
    ~DynArray()
    {
        delete[] mArray;
    }

    // Copy constructor
    //      Used to initialize one object to another
    // In:  _da             The object to copy from
    DynArray(const DynArray& _da)
    {
        *this = _da;
    }

    // Assignment operator
    //      Used to assign one object to another
    // In:  _da             The object to assign from
    //
    // Return: The invoking object (by reference)
    //      This allows us to daisy-chain
    DynArray& operator=(const DynArray& _da) {
    
    }

    // Overloaded [] operator
    //      Used to access an element in the internal array (read-only)
    // In:  _index          The index to access at
    //
    // Return: The item at the specified index (by reference)
    const Type& operator[](int _index) const 
    {
    
    }

    // Overloaded [] operator
    //      Used to access an element in the internal array (writeable)
    // In:  _index          The index to access at
    //
    // Return: The item at the specified index (by reference)
    Type& operator[](int _index) {
        
    }

    // Get the current number of elements actively being used
    //
    // Return: The current number of elements used
    int Size() const {
        
    }

    // Get the current capacity of the internal array
    //
    // Return: The capacity of the array
    int Capacity() const {
        
    }

    // Clear the class for re-use
    //          Should clean up all dynamic memory and leave the object the same as if the default constructor had been called
    void Clear() {
        
    }

    // Add an item to the end of the array
    //          Should resize the array if needed
    // In:  _data           The item to be added
    void Append(const Type& _data) {
        
    }

    // Resizes the internal array, and copies all data over
    // In: _newCapacity     The new capacity of the array
    //  NOTE:   If 0 is passed, the array should double in size
    //          If _newCapacity < mCapacity, do nothing
    //
    //  SPECIAL CASE: If mCapacity is 0, then it should be set to 1
    void Reserve(unsigned int _newCapacity = 0)
    {
        
        if (_newCapacity < mCapacity)
        {
            continue;
        }
        if (mCapacity = 0)
        {
            mCapacity = 1;
        }
    }

};
#endif

【问题讨论】:

  • 我认为“继续”对于底部的一件事来说非常接近
  • if (mCapacity = 0) 您缺少用于比较的等号==。现在它每次都分配0。你也不能在循环之外继续。
  • omg bless
  • 哦,已经设置的字段是代码提供给我们的方式,所以这就是为什么有字段但它们是空的
  • 根据您提供的代码,在您获得正确的创建和复制语义之前,不要尝试实现任何其他功能。这意味着您实现了构造函数、复制构造函数、析构函数,然后是赋值运算符。然后测试复制 DynArray 以确保您不会出现内存泄漏/崩溃等问题。一旦您有了它,然后只有这样您才尝试进行附加、清除等操作。

标签: c++ pointers visual-c++ memory-management


【解决方案1】:

让我们修复您尝试过的问题。它说明了你的许多误解。

所以想想需要什么。

  1. 我们必须创建一个具有给定容量的数组,这意味着分配给定数量的元素并将它们分配给mArray指针

  2. 我们必须将数组容量设置为给定值。

  3. 创建数组后大小将为零,因此我们必须将大小设置为零。

综合起来我们得到

DynArray(unsigned int _startingCap = 0)
{
    // allocate memory for the given capacity
    mArray = new Type[_startingCap];
    // set the array capacity to the given value
    mCapacity = _startingCap;
    // set the size to zero
    mSize = 0;
}

我不知道从哪里开始您的代码。您已经清楚地从某个地方复制了一些字符串代码并试图使其适应新情况。但是这段代码与字符串无关,甚至忘记了字符串和数组之间的区别,你复制的代码在做一些完全不同的事情。

您了解大小和容量的概念,以及它们之间的区别吗?你上面写的代码表明你没有。在你继续之前,你真的必须理解这个概念。

数组的大小是数组有多少有效元素。很简单。

数组的容量是它分配了多少内存。一个数组的容量可以大于它的大小(但显然不能少)。额外的容量允许数组增长(即增加它的大小),而无需重新分配保存数组元素的内存。

【讨论】:

  • 哦,就像我说的那样,括号中的代码是我们在为我们提供的模板代码中给出的代码
  • 我们必须在使用这些排序作为骨架而不是建立起来
  • @yallneedsomebleach 给出的代码甚至不合法。 Array 是什么,它没有贴在任何地方。 strlen(_startingCap) 不合法,因为 _startingCap 是整数,而不是字符串。等等等等。如果那是您应该使用的代码,那么就出现了严重错误。我会和你的老师谈谈,因为那个代码根本没有意义。
  • @yallneedsomebleach 很抱歉认为代码是你的,当它是你给出的代码时。
  • @yallneedsomebleach 你是否也给了这个代码*this = _da;?因为那个代码也是错误的。这里发生了一些奇怪的事情。
【解决方案2】:

这里是一个更新的版本,建议已实施 ^U^ 模板 类 DynArray {

friend class TestSuite; // Giving access to test code

Type* mArray;
unsigned int mSize;
unsigned int mCapacity;

公开:

// Default constructor
//      Creates a new object
// In:  _startingCap        An initial capacity  to start the array at (optional)
DynArray(unsigned int _startingCap = 0)
{

    
    mArray = new Type[_startingCap];
    mCapacity = _startingCap;
    mSize = 0;

}


// Destructor
//      Cleans up all dynamically allocated memory
~DynArray()
{
    delete[] mArray;
}

// Copy constructor
//      Used to initialize one object to another
// In:  _da             The object to copy from
DynArray(const DynArray& _da)
{
    operator = (_da)
}

// Assignment operator
//      Used to assign one object to another
// In:  _da             The object to assign from
//
// Return: The invoking object (by reference)
//      This allows us to daisy-chain
DynArray& operator=(const DynArray& _da) {

}

// Overloaded [] operator
//      Used to access an element in the internal array (read-only)
// In:  _index          The index to access at
//
// Return: The item at the specified index (by reference)
const Type& operator[](int _index) const 
{

}

// Overloaded [] operator
//      Used to access an element in the internal array (writeable)
// In:  _index          The index to access at
//
// Return: The item at the specified index (by reference)
Type& operator[](int _index) {
    
}

// Get the current number of elements actively being used
//
// Return: The current number of elements used
int Size() const 
{
    return Size;
}

// Get the current capacity of the internal array
//
// Return: The capacity of the array
int Capacity() const {
    
}

// Clear the class for re-use
//          Should clean up all dynamic memory and leave the object the same as if the default constructor had been called
void Clear() {
    
}

// Add an item to the end of the array
//          Should resize the array if needed
// In:  _data           The item to be added
void Append(const Type& _data) {
    
}

// Resizes the internal array, and copies all data over
// In: _newCapacity     The new capacity of the array
//  NOTE:   If 0 is passed, the array should double in size
//          If _newCapacity < mCapacity, do nothing
//
//  SPECIAL CASE: If mCapacity is 0, then it should be set to 1
void Reserve(unsigned int _newCapacity = 0)
{
    
    if (_newCapacity < mCapacity)
    {
        continue;
    }
    if (mCapacity == 0)
    {
        mCapacity = 1;
    }
}

};

【讨论】:

  • 您的复制构造函数实际上与您之前的版本相同,并且错误方式完全相同。从第一原则编写复制构造函数,它需要分配一些内存,然后从_da 复制数据。与其寻找一些可以神奇地工作的代码,不如考虑复制构造函数在分配内存和复制数据方面需要做什么,然后实现它。这里没有捷径。
  • 哦,我明白了,让我试试
  • 所以我复制了项目顶部的定义信息(unsinged int 部分)并将其放入我的副本中,如下所示 // 复制构造函数 // 用于初始化一个对象到另一个 // In: _da 要复制的对象 from DynArray(const DynArray& _da) { mArray = new Type[_startingCap]; mCapacity = _startingCap; mSize = 0; }
  • 那么在该代码中,您从_da 复制到哪里?你写的代码甚至没有提到_da
  • 事实上,代码甚至无法编译,因为_startingCap 是一个未声明的变量。如何编译这段代码?你不能说它不编译吗?
猜你喜欢
  • 2012-10-02
  • 2017-12-13
  • 1970-01-01
  • 2011-07-19
  • 2013-10-03
  • 2010-10-30
  • 2021-07-11
  • 2022-10-25
  • 1970-01-01
相关资源
最近更新 更多