调整动态数组 c++ 的大小

Resizing a dynamic array c++

本文关键字:c++ 动态 数组 调整      更新时间:2023-10-16

我目前正在处理一项作业,您应该在其中从文件中读取球体。文件中的典型输入可能如下所示:"0.182361 -0.017057 0.129582 0.001816"。表示 x、y 和 z 坐标加上球体半径。在读取文件时,我正在使用自己的方法:"AddSphere",它将球体添加到数组中。

void Array::AddSphere(MySphere inp)
{
if (length == 10000 || length == 200000 || length == 30000)
{
    ResizeBuffer(); 
}
this->length++;
*arr = inp;
arr++;
//this->length++;
}

类"Array"应该像所有球体的持有者,并且包含指向当前元素的变量"arr"。

    class Array
    {
    public :
    int length;
    void AddSphere(MySphere inp);
    int arrLength();
    void RemoveAt(int pos);
    void AddFromFile();
    MySphere * Buffer;
    void CreatenewBuffer();
    private:
    MySphere * arr;
    public:Array()
       {
           Buffer = new MySphere[10000];
           arr = Buffer;
           length = 0;
       }
       ~Array()
       {
           delete[] arr;
           delete[] Buffer;
       }
};

它还包含指向"arr"中第一个元素的"缓冲区"。那么现在我的问题是什么?问题是我希望能够在"长度"等于指定值时动态增加缓冲区的大小。假设我的文件包含 15000 个元素。然后我需要增加缓冲区大小,以便能够在数组上具有正确的长度。

    void Array::ResizeBuffer()
{
    MySphere * tempBuff = new MySphere[length+10000];
    memcpy(tempBuff, Buffer, sizeof((MySphere)*Buffer));
    this->Buffer = new MySphere[length+10000];
    arr = Buffer;
    memcpy(Buffer, tempBuff, sizeof((MySphere)*tempBuff));

    //int a = 0;
    }

但是由于某种原因,我只在输出中获得了最后 5000 个元素,而不是所有 15000 个元素。我认为这与 arr 指针没有指向整个缓冲区有关,但我的尝试都没有奏效。那么为什么会这样呢?

谢谢你的时间!

几个问题:

动态数据只分配一次(尽管您有两个指向 arrea 的指针)。

Array()
   {
       Buffer = new MySphere[10000];
       arr = Buffer;
       length = 0;
   }

因此,您只能删除一次(一次分配一次销毁)。

   ~Array()
   {
       delete[] arr;
       delete[] Buffer;  // Double delete.
   }

复制对象时,不能使用 memcpy(除非对象属于非常特殊的子类别)。如果要将C++与方法及其所有其他功能一起使用,则不太可能出现这种情况。更喜欢使用 std::copy() 将内容从一个数组复制到另一个数组。

MySphere * tempBuff = new MySphere[length+10000];
memcpy(tempBuff, Buffer, sizeof((MySphere)*Buffer));
this->Buffer = new MySphere[length+10000];
arr = Buffer;
memcpy(Buffer, tempBuff, sizeof((MySphere)*tempBuff));

还有你为什么要复制两次?

除了使用 std::copy
之外的注意事项调整大小应分三个不同的阶段进行。

1. Allocate and copy data       // Dangerous may throw
2. Reset object                 // Safe
3. Release old resources.       // Dangerous may throw

所以这就是它应该的样子。

// Phase 1: Allocate and copy.
std::unique_ptr<MySphere> tempBuffer = new MySphere[length+1000];   // Hold in a smart pointer
                                                                    // For exception safety
// Prefer to use std::copy. It will use the objects copy constructor
std::copy(Buffer, Buffer+length, MySphere);                         // Copies the objects correctly. 

// Everything worked so Phase 2. Reset the state of the object.
// We can now put the tempBuffer into the object
MySphere* toDelete = Buffer;                                        // keep old buffer for stage 3.
Buffer = tempBuffer.release();
length += 1000;
// Phase 3
// State of the new object is consistent.
// We can now delete the old array
delete [] toDelete;

如果您不被允许使用智能指针。然后将阶段 1 替换为以下内容:

// Phase 1: Allocate and copy.
MySphere* tempBuffer = new MySphere[length+1000];
try                                                 // For exception safety
{
    // Prefer to use std::copy. It will use the objects copy constructor
    std::copy(Buffer, Buffer+length, MySphere);     // Copies the objects correctly. 
}
catch(...)
{
    delete [] tempBuffer;
    throw;
}

由于您的类已获得动态分配对象的所有权。你的类也应该实现三规则。这意味着您需要定义复制构造函数和赋值运算符。

拥有动态数组真正需要的是三件事:

    1) A pointer to the buffer.
    2) The current size the user knows about (reported by size)
    3) The actual size. At which point you need to resize

下面是如何为数组实现三法则的示例:

https://stackoverflow.com/a/255744/14065

其他任何事情都是多余的。

在您的情况下是不必要的,因为这是一个项目。但是对于额外的分数,请查找如何使用放置新。这应该可以防止对数组中实际上不存在的对象进行额外的初始化。

如何使用 place new 的示例:

https://stackoverflow.com/a/13994853/14065

你没有更新 Array::CreatenewBuffer() 函数中的长度变量。

我假设您在哪里迭代对象,您可能有这样的东西......

for (size_t i = 0; i < Array.length; ++i)
    printf("Item found");

现在我回头看,你使用长度的方式实际上相当混乱。您可能希望将其分为两个变量:大小和容量。容量是当前分配的缓冲区可以容纳的对象数量,大小是实际添加到缓冲区的对象数量。

这个怎么样?

void Array::CreatenewBuffer()
{
    MySphere * tempBuff = new MySphere[length+10000];
    std::copy ( Buffer, Buffer+length, tempBuff );
    delete[]Buffer;
    Buffer = tempBuff;
    delete[]tempBuff;
}

每个球体的值是否在不同的行上输入?如果是这样,您可以简单地计算行数,然后使用正确的值初始化缓冲区。