Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++:扩展数组的函数_C++_Arrays_Memory Management - Fatal编程技术网

C++:扩展数组的函数

C++:扩展数组的函数,c++,arrays,memory-management,C++,Arrays,Memory Management,在问这个问题之前,我看了一下并试图用它的代码来解决我的问题,但它没有起作用 我正在尝试编写一个函数来扩展给定的静态对象数组。以下是基于上述链接问题代码的当前功能: void MyClass::expand_array(MyClass *&old_array, int &old_array_size, int new_slots) { MyClass *transfer_array = new MyClass[old_array_size + new_slots];

在问这个问题之前,我看了一下并试图用它的代码来解决我的问题,但它没有起作用

我正在尝试编写一个函数来扩展给定的静态对象数组。以下是基于上述链接问题代码的当前功能:

void MyClass::expand_array(MyClass *&old_array, int &old_array_size,
    int new_slots)
{
    MyClass *transfer_array = new MyClass[old_array_size + new_slots];

    for (int i = 0; i < old_array_size; i++) {
        transfer_array[i] = old_array[i];
    }

    delete[] old_array;
    old_array = transfer_array;
    transfer_array = NULL;
}

输出的最后一行来自尝试删除数组的最后一次迭代。

扩展数组的第一个参数定义为myclass*&,查看代码,我认为您需要将其设为myclass**并调整调用代码。我猜您正在声明myclass a[10]并试图传递它,但这永远不会起作用-如果要删除它,您必须传递分配给new的数组

您似乎与数组指针等价性有冲突。在本文中,我指的是C数组,例如

MyClass a[10];
在C++和S+C++中,数组可以衰减为指针。
void f(int input[]);
该语言没有为这个函数提供机制来告诉它接收了多少,因此为了函数调用的目的,上面的数组衰减为一个指针。也就是说,在上述函数体中,输入类型为int*

在其他地方也会出现这种情况,以允许我们对数组进行指针数学运算(无数组)和类似指针的操作:

char a[10];
sizeof(a); // understands a is really an array and evaluates to 10
sizeof(*a); // decays a to a pointer and returns sizeof(char)
char* b = a + 1; // equivalent to b = &a[1];
b[1] = 0; // equivalent to *(b + 1) = 0;
但是,您正在尝试引用指针。在这种情况下,阵列不会衰减,请考虑:

void f(int*& ptr)
{
    ptr = nullptr;
}

void x()
{
    int a[16];
    f(a);
}
a不是指向堆栈上16个整数的指针,而是16个整数。从根本上说,我们不能重新安置a

如果需要可调整大小的数组,则需要使用分配:

MyClass* array = new MyClass[10];
MyClass::expand_array(array, 10, 20);
delete [] array;
尽管如此,我想指出,这种方法非常糟糕,因为它将指针暴露给消费者。相反,您应该将数组大小和当前存储位置的跟踪移动到MyClass中。也许看看

但总的来说,更像这样:


有关数组指针等价性的更多信息,请参阅。

错误与调用函数的内容有关。第一个参数引用一个指针,但您正在提供一个数组。不相关,但是考虑返回新数组。这是更好的自我记录。如果是我的代码,我根本不会修改旧的数组指针。但是,每个数组都有自己的名称。@0x499602D2那么,将数组传递给函数的正确方法是什么呢?@jpaugh原始数组需要通过几个这样的操作来保持相同的名称,我认为返回时不可能做到这一点。而且,我不知道你可以在C++中返回一个数组。你不能把指针返回到任何东西吗?但我离题了。。C++不是我的强项;祝你好运。如果它是一个MyClass*,他将无法为它分配数组。我已经尝试过这样做,但它仍然无法编译。我已经在代码的其他地方使用了这种精确的方法:MyClass*数组=新MyClass[10];MyClass::expand_arrayarray,10,20;删除[]数组;。但它不起作用。@徽章你说它不起作用是什么意思?您需要详细说明,以便我们可以帮助您。@0x499602D2代码没有使用问题中显示的错误消息进行编译。@kfson不会将数组衰减为指针,但将其绑定到引用会失败,因为指针是临时右值,而引用只接受左值?@0x499602D2请忽略我之前的语句,然后重试给我一点时间编译。我使用的是一个静态数组,而不是指向一个数组的指针,这是我出现问题的原因。
MyClass* array = new MyClass[10];
MyClass::expand_array(array, 10, 20);
delete [] array;
#include <functional>
#include <cstdint>
#include <iostream>

// CAVEAT EMPTOR: This is a partial, naive implementation,
// it lacks copy/move constructor, operator=, iterators and
// many other things. It is solely intended to demonstrate
// internalizing the paired-state components of size and data
// to provide a discrete encapsulation of the concept of a
// dynamically sized array-like allocation.

class MyArray
{
    // create an alias for the type we're using,
    // this will come in handy when you learn templates.
    typedef int value_type;

    value_type* m_data;
    size_t m_size;

public:
    MyArray() : m_data(nullptr), m_size(0) {}
    MyArray(size_t defaultSize) : m_data(nullptr), m_size(0)
    {
        resize(defaultSize);
    }

    void resize(size_t newSize)
    {
        if (newSize > size()) {
            value_type* newData = new value_type[newSize];
            if (m_data) {
                std::copy(m_data, m_data + size(), newData);
                delete [] m_data;
            }
            m_data = newData;
        }
        m_size = newSize;
        if (m_size == 0 && m_data) {
            delete [] m_data;
            m_data = nullptr;
        }
    } 

    value_type operator[](size_t index) const { return m_data[index]; }
    value_type& operator[](size_t index) { return m_data[index]; }

    size_t size() const { return m_size; }

    void push_back(value_type newValue)
    {
        resize(m_size + 1);
        m_data[size() - 1] = newValue;
    }
};

int main()
{
    MyArray a;
    a.resize(4);
    for (size_t i = 0; i < a.size(); ++i) {
        a[i] = i;
    }

    a.push_back(10);

    std::cout << "Values:\n";
    for (size_t i = 0; i < a.size(); ++i) {
        std::cout << i << ": " << a[i] << '\n';
    }

    return 0;
}