Почему «fread: EFAULT, bad address» в этой функции C? - PullRequest
0 голосов
/ 19 февраля 2019

Следующая функция читает произвольные данные из stdin и выдает unsigned int по одному разу.Если он находит EOF, он выходит.Если файл бесконечен (например, /dev/urandom), он продолжается вечно.У меня есть файл около 95 МиБ, который заставляет его производить fread: Bad address.Вы понимаете почему?

unsigned int generator(void)
{
  static unsigned int buffer[8192 / sizeof(unsigned int)];
  static unsigned int pos; /* where is our number? */
  static unsigned int limit; /* where does the data in the buffer end? */

  if (pos >= limit) {
    /* refill the buffer and continue by restarting at 0 */
    limit = fread(buffer, sizeof (unsigned int), sizeof buffer, stdin);

    if (limit == 0) {
      /* We read 0 bytes.  This either means we found EOF or we have
      an error.  A decent generator is infinite, so this should never
      happen. */
      if (ferror(stdin) != 0) {
        perror("fread"); exit(-1);
      }
      if (feof(stdin) != 0) {
        printf("generator produced eof\n");
        exit(0);
      }
    }

    pos = 0;

  }

  unsigned int random = buffer[pos]; /* get one */
  pos = pos + 1;
  return random;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...