【问题标题】:C++ - Segmentation Fault - Dynamic array [closed]C++ - 分段错误 - 动态数组
【发布时间】:2014-10-27 19:01:59
【问题描述】:

我一直在尝试用 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;

}

【问题讨论】:

  • [OT]:*(temp + x) 和 array[x] 表示法之间可能是一致的。
  • “我不想使用像 vector 这样的 STL 容器。” 所以基本上你是在说“我不想使用提供给我的经过验证的高效实现,但宁愿利用我的时间来实现一些可能效果不佳或速度不快的东西。”标准库的存在是有原因的。 使用它。
  • 嗨 cdhowie,我没有使用矢量,因为这个问题与我正在进行的练习有关,矢量是限制之一。在以后的项目中,我肯定会使用vector。谢谢!

标签: c++ arrays pointers segmentation-fault


【解决方案1】:

array = temp; 在传递 unsigned int* array 而不是 unsigned int*&amp;array 时没有按照您的预期进行

将原型替换为

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

另外,你应该初始化你的变量:

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

并在 main 中删除您的 array:

delete [] array;

【讨论】:

  • 非常感谢!这段代码 unsigned "int*& array 解决了"问题。
【解决方案2】:

您没有初始化指针数组或变量size。所以程序有未定义的行为

函数也必须声明为

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

或

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

否则main中定义的指针不会改变。

例如

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;

}

考虑到在标头&lt;algorithm&gt; 中声明的标准算法std::copy 和std::copy_n 可以用来代替函数内的循环。例如

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

或

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

【讨论】:

  • 你错过了 main 中的最后一个 delete [] array;。
  • @Jarod42 不是我错过了删除。这是问题的作者。我不确定这个测试程序是否需要删除。
  • 当您重写代码来修复它时,最好显示所有(非风格)修复。这更加强调了使用vector 更容易:-)
  • @Jarod42 好吧,如果你坚持我已经添加了删除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 1970-01-01
相关资源
最近更新 更多