简体   繁体   English

尝试将数组从stdin复制到2d数组时,为什么会出现分段错误?

[英]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.

相关问题 为什么在尝试打印数组时出现分段错误? - Why do I get segmentation fault when trying to print an array? 尝试将stdin读取到2d动态分配的数组时出现分段错误 - Segmentation fault when trying to read stdin to a 2d dynamically allocated array 尝试从2D数组中的字符串获取字符时出现分段错误 - Segmentation fault when trying to get a character from a string in a 2D array 为什么在 c 中声明二维数组会导致分段错误? - Why do I get a segmentation fault by declaring a 2d array in c? 尝试调整2D阵列大小时出现分段错误 - Segmentation fault when trying to resize a 2D Array 尝试连接二维数组的元素时出现分段错误 - Segmentation fault when trying to concatenate an element of a 2d array 尝试从文件读入二维数组时出现分段错误 - segmentation fault while trying to read in from file to a 2D array 尝试通过指针数组从二维数组打印字符串时出现分段错误 - Segmentation fault when trying to print strings from 2D array via array of pointers 尝试从数组复制时出现分段错误(核心转储)错误 - segmentation fault (core dumped) error when trying to copy from an array 二维数组中的分段错误 - segmentation fault in 2d array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM