我一直在尝试用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;
}
答案 0 :(得分:2)
array = temp;
在您通过unsigned int* array
而不是unsigned int*&array
用
替换原型void addElement(int newElement, unsigned int*& array, int& size )
此外,您应该初始化变量:
unsigned int* array = nullptr;
int size = 0;
并删除main中的array
:
delete [] array;
答案 1 :(得分:1)
你没有初始化指针数组和变量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;
}
考虑到在标头std::copy
中声明的标准算法std::copy_n
和<algorithm>
可以在函数中使用而不是循环。例如
std::copy_n( array, size, temp );
或
std::copy( array, array + size, temp );