C++-分段故障-动态数组

C++ - Segmentation Fault - Dynamic array

本文关键字:动态 数组 故障 分段 C++-      更新时间:2023-10-16

我一直在尝试用C++实现一个动态数组,但在指针引用方面遇到了一些问题。

使用以下代码,我得到了一个分段错误。

如果我使用数组和大小作为全局变量,那么应用程序就可以工作了。我知道我可以做到,但我想知道为什么它不起作用。我想我正在丢失指针数组指针引用,但我不知道如何在方法中保留它。

我不想像vector那样使用STL容器。

谢谢你的帮助。

void addElement(int newElement, unsigned int * array, int& size ){
    /* create a temp array in order to copy the contents from the current array
    * it is needed because we want to increase the size of the array */
    unsigned int* temp = new unsigned int[size + 1];
   // copy all the data from array to temp
   for (int x=0; x < size; x++){
       *(temp + x) = array[x];
   }
   // add the new element
   *(temp + size) = newElement;
   size++;
   // release the memory from the temp array
   delete [] array;
   // the temp array turn into the temp array, that contains all the elements
   array = temp;
}
int main(){
    unsigned int * array;
    int size;
    addElement(1, array, size);
    addElement(2, array, size);
    addElement(3, array, size);
    cout << array[1];
    return 1;
}

array = temp;在通过unsigned int* array而不是unsigned int*&array 时不会达到预期效果

用替换原型

void addElement(int newElement, unsigned int*& array, int& size )

此外,您应该初始化您的变量:

unsigned int* array = nullptr;
int size = 0;

并删除你的array在主:

delete [] array;

您既没有初始化指针数组,也没有初始化变量size。因此程序具有未定义的行为

此外,该函数必须声明为

void addElement(int newElement, unsigned int * & array, int& size );

int * addElement(int newElement, unsigned int * array, int& size );

否则,main中定义的ponter将不会更改。

例如

int * addElement(int newElement, unsigned int * array, int& size ){
    /* create a temp array in order to copy the contents from the current array
    * it is needed because we want to increase the size of the array */
    unsigned int* temp = new unsigned int[size + 1];
   // copy all the data from array to temp
   for (int x=0; x < size; x++){
       *(temp + x) = array[x];
   }
   // add the new element
   *(temp + size) = newElement;
   size++;
   // release the memory from the temp array
   delete [] array;
   // the temp array turn into the temp array, that contains all the elements
   return temp;
}
int main(){
    unsigned int * array = NULL;
    int size = 0;
    array = addElement(1, array, size);
    array = addElement(2, array, size);
    array = addElement(3, array, size);
    cout << array[1];
    delete [] array;
    return 1;
}

void addElement(int newElement, unsigned int * &array, int& size ){
    /* create a temp array in order to copy the contents from the current array
    * it is needed because we want to increase the size of the array */
    unsigned int* temp = new unsigned int[size + 1];
   // copy all the data from array to temp
   for (int x=0; x < size; x++){
       *(temp + x) = array[x];
   }
   // add the new element
   *(temp + size) = newElement;
   size++;
   // release the memory from the temp array
   delete [] array;
   // the temp array turn into the temp array, that contains all the elements
   array = temp;
}
int main(){
    unsigned int * array = NULL;
    int size = 0;
    addElement(1, array, size);
    addElement(2, array, size);
    addElement(3, array, size);
    cout << array[1];
    delete [] array;
    return 1;
}

考虑到在头<algorithm>中声明了标准算法std::copystd::copy_n,它们可以用来代替函数中的循环。例如

std::copy_n( array, size, temp );

std::copy( array, array + size, temp );