[英]Why do I get a segmentation fault when trying to copy an array from stdin to a 2d array?
This is in relation to the HackerRank Restaurant problem, which I have solved in other languages but am trying, now, to solve in C. https://www.hackerrank.com/challenges/restaurant 这是相对于HackerRank餐厅的问题,这是我在其他语言中已经解决了,但我想,现在,在C.解决https://www.hackerrank.com/challenges/restaurant
I first tried to store the result if read_slice_dimension()
directly into the multidimensional array, but because I cannot pass an array back from a function without making it static, this does not work because then every nested array points to the same static 2 integer array in memory, which, as a result of it being overwritten every time read_slice_dimension()
is called, means I would have an array containing num_slices
pointers to the last array read in from stdin. 我首先尝试将read_slice_dimension()
的结果直接存储到多维数组中,但是由于我无法在不使其静态的情况下将其从函数传递回数组,因此这是行不通的,因为每个嵌套数组都指向同一静态2整数数组在内存中,由于每次调用read_slice_dimension()
时read_slice_dimension()
覆盖该内存,这意味着我将拥有一个包含num_slices
指针的数组, num_slices
指针指向从stdin读取的最后一个数组。
Thus my decision to try memcpy
, so that I could copy the arry from read_slice_dimension()
to a new block of memory so that it persists and is not lost when I read in the next slice. 因此,我决定尝试使用memcpy
,这样我就可以将read_slice_dimension()
从read_slice_dimension()
复制到新的内存块中,以便它持久存在,并且在我读取下一个切片时不会丢失。 It appears, however, that memcpy
is not the way to do this. 但是,看来memcpy
并不是做到这一点的方法。 What is? 什么是?
// Gets the number of slices for this test according to the first input value from stdin.
int read_num_slices() {
int num_slices = 0;
scanf("%i", &num_slices);
if (num_slices == 0) {
goto error;
}
return num_slices;
error:
flag_error("ERROR: Could not parse the number of entries from first input line.");
}
// Gets a single line from stdin and attempts to parse it into a 2D int array representing the dimensions of a slice.
int* read_slice_dimension() {
static int slice_dimension[2] = {0};
scanf("%i %i", &slice_dimension[0], &slice_dimension[1]);
if (slice_dimension[0] + slice_dimension[1] == 0) {
goto error;
}
return slice_dimension;
error:
flag_error("ERROR: Could not parse line entered into a 2 integer array representing the slice's dimensions.");
}
// Gets all of the bread slices to be processed.
//
// This function reads from stdin. The first line should be a single integer that specifies the number of slices to be
// processed by this current test. The subsequent lines should be two integers separated by a space which represent
// the 2D dimensions of each slice.
int** get_slices() {
int num_slices = read_num_slices();
static int** slices;
slices = (int**)malloc(num_slices * sizeof(int*));
int i = 0;
for (i; i < num_slices; i++) {
int* slice = slices[i];
slice = (int*)malloc(2 * sizeof(int));
memcpy(slice, read_slice_dimension(), 2 * sizeof(int));
printf("%i %i\n", slices[i][0], slices[i][1]); // CAUSES SEGMENTATION FAULT
}
return slices;
}
You're setting slice = slices[i]
, then calling malloc
. 您正在设置slice = slices[i]
,然后调用malloc
。 That's backwards. 那是倒退。 Call malloc
, then set slices[i]
. 调用malloc
,然后设置slices[i]
。 – user3386109 – user3386109
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.