C++-分段故障-动态数组
C++ - Segmentation Fault - Dynamic array
我一直在尝试用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::copy
和std::copy_n
,它们可以用来代替函数中的循环。例如
std::copy_n( array, size, temp );
或
std::copy( array, array + size, temp );
相关文章:
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 输出没有重复元素的动态数组(收缩数组)C++
- 正在插入动态数组
- 如何在动态数组上使用搜索函数
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 静态数组的自由动态数组
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- 为什么 c++ 动态数组的大小没有改变?
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- 指针的 C++ 动态数组 - 何时需要使用它?
- 删除动态数组时未定义标识符
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 动态数组的 C++ 重载加运算符
- 基于数组的列表 - 动态数组创建时出错