[英]Segmentation fault when passing a pointer to an array into a function (C++)
I'm trying to get a large array of random numbers, then pass that array into a function that will create a new array with 10 elements whose values correspond with the values in the large array. 我正在尝试获取一个大的随机数数组,然后将该数组传递给一个函数,该函数将创建一个包含10个元素的新数组,这些元素的值与该大数组中的值相对应。 I pass the parameter start into the function to indicate the index of the large array from which I will start copying the values into the smaller array. 我将参数start传递给函数,以指示大型数组的索引,从该索引开始将值复制到较小的数组中。
In case my explanation isn't clear, I have an array A of 20 randomly generated numbers: 如果我的解释不清楚,我有一个数组A,其中包含20个随机生成的数字:
A = {1, 2, 3, .... 20} A = {1,2,3,.... 20}
I want to make an array B that holds the first 10 numbers: 我想制作一个包含前10个数字的数组B:
B = {1, 2, 3, .... 10} B = {1,2,3,.... 10}
and an array C that holds the second 10: 和一个数组C,存放第二个10:
C = {11, 12, 13, .... 20} C = {11,12,13,.... 20}
I'm able to generate the random numbers and display them with a for loop, but once I start trying to make the new arrays I get a segmentation fault in terminal: 我能够生成随机数并使用for循环显示它们,但是一旦我开始尝试制作新的数组,我就会在终端中遇到分段错误:
Segmentation fault (core dumped) 分段故障(核心已转储)
Below is my code. 下面是我的代码。
int main() {
int size = 20;
int *arrA = getRandomScores(size);
int *arrB = applyScores(arrA, 0);
int *arrC = applyScores(arrA, 10);
for(int i = 0; i < 10; i++) {
cout << arrB[i] << endl;
}
cout << endl << endl;
for(int i = 0; i < 10; i++) {
cout << arrC[i] << endl;
}
return 0;
}
int *applyScores(int *arr, int start) {
int *newArr;
for(int i = 0; i < 10; i++) {
newArr[i] = arr[start];
start++;
}
return newArr;
}
int *getRandomScores(int size) {
int *arr;
//return null if size is zero or negative
if (size <= 0)
return NULL;
//dynamically allocate the array
arr = new int[size];
//seed the random number generator
srand(time(0));
//populate the array with random numbers
for(int i = 0; i < size; i++)
arr[i] = rand() % 100;
return arr;
}
I'm on linux, so I was told to use valgrind to see a more detailed error message. 我在linux上,因此被告知使用valgrind查看更详细的错误消息。 Below is what valgrind gave me. 以下是valgrind给我的东西。 This is my first encounter with a segmentation fault, so forgive me if the answer is trivial with valgrind's help. 这是我第一次遇到细分错误,如果在valgrind的帮助下答案不重要,请原谅我。
You declare newArr
as a pointer to int
, but it points nowhere. 您将newArr
声明为指向int
的指针,但它没有指向任何地方。 You could create a new array like this: 您可以创建一个新的数组,如下所示:
int *newArr = new int[10];
but remember to delete the array when you are done with it: 但请记住在完成操作后删除该数组:
delete[] arrB;
delete[] arrC;
Or you could simply use std::vector<int>
for your arrays A,B and C (or at least B and C). 或者,您可以简单地将std::vector<int>
用于数组A,B和C(或至少B和C)。
you never allocate the memory for B and C 您永远不会为B和C分配内存
int *applyScores(int *arr, int start) {
int *newArr = new int[10];
for(int i = 0; i < 10; i++) {
newArr[i] = arr[start];
start++;
}
return newArr;
}
As valgrind states (use of uninitialised value, which means the pointer and invalid write, which means write to where the pointer happens to refer to): you don't allocate memory to copy the random scores to arrB
and arrC
. 作为valgrind状态(使用未初始化的值,这意味着指针,而无效的写入,这意味着指向指针恰好指向的位置进行写操作):您不分配内存将随机分数复制到arrB
和arrC
。 arrNew
in applyScores()
is just a pointer, so it can't store an array. arrNew
applyScores()
中的applyScores()
只是一个指针,因此它不能存储数组。 The easiest way to avoid this kind of problems is to use standard library containers like std::vector
or std::array
, which do what you expect and avoid manual memory management. 避免此类问题的最简单方法是使用标准库容器,如std::vector
或std::array
,它们可以实现您所期望的并避免手动内存管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.