C: realloc 适用于 Linux,但不适用于 Windows

标签 c linux windows algorithm sorting

这是我在 Stack Overflow 上的第一个问题,如果写得不好请见谅。 我有一个小问题。我用 C 编写了一个程序(我目前正在学习 C,我是新手,我的第一语言,请不要说我应该学 Python,因为我用 C 做得很好)。所以,我写了这个小程序。这是我尝试实现一个排序算法(算法是我自己做的,没有任何帮助和文档,我觉得效率很低,我只是在胡闹,虽然我不知道算法是否已经存在)。我知道的唯一排序算法是 QuickSort。 无论如何,这是最终程序(有很多评论,以帮助我记住它是如何工作的,如果我再次访问它的话):

// trying to implement my own sorting algorithm
// it works the following way:
// for an array of n integers, find the largest number,
// take it out of the array by deleting it, store it
// at the very end of the sorted array.
// Repeat until the original array is empty.
// If you need the original array, simply
// make a copy of it before sorting
/***************************************/
// second implementation
// same sorting algorithm
// main difference: the program automatically
// computes the number of numbers the user enters

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int *sort(int *a, int n);                         // sort: the actual sorting function
char *read_line(char *str,int *num_of_chars);     // read_line: reads input in string form
int *create_array(char *str, int n);              // create_array: counts the num of integers entered and extracts them
                                                  // from the string the read_line function returns, forming an array
int size_of_array_to_be_sorted = 0;               // of integers

int main(void)
{
  int *array, i, *sorted_array, size = 3;
  char *str = malloc(size + 1);

  if (str == NULL)
  {
    printf("\nERROR: malloc failed for str.\nTerminating.\n");
    exit(EXIT_FAILURE);
  }
  printf("Enter the numbers to be sorted: ");
  str = read_line(str, &size);

  array = create_array(str, size + 1);
  sorted_array = sort(array, size_of_array_to_be_sorted);

  printf("Sorted: ");
  for (i = 0; i < size_of_array_to_be_sorted; i++)
    printf("%d ", sorted_array[i]);
  printf("\n\n");

  return 0;
}

int *sort(int *a, int n)
{
  int i, j, *p, *sorted_array, current_max;

  sorted_array = malloc(n * (sizeof(int)));
  if (sorted_array == NULL)
  {
    printf("ERROR: malloc failed in sort function.\nTerminating.\n");
    exit(EXIT_FAILURE);
  }

  for (i = n - 1; i >= 0; i--)      // repeat algorithm n times
  {
    current_max = a[0];             // intiliaze current_max with the first number in the array
    p = a;
    for (j = 0; j < n; j++)         // find the largest integer int the array
      if (current_max < a[j])
      {
        current_max = a[j];
        p = (a + j);                // make p point to the largest value found
      }
    *p = INT_MIN;                        // delete the largest value from the array
    sorted_array[i] = current_max;       // store the largest value at the end of the sorted_array
  }

  return sorted_array;
}

char *read_line(char *str, int *num_of_chars)
{
  int i = 0;     // num of chars initially
  char ch, *str1 = str;

  while ((ch = getchar()) != '\n')
  {
    str1[i++] = ch;
    if (i == *num_of_chars)                             // gives str the possibility to
    {                                                   // dinamically increase size if needed
       str1 = realloc(str, (*num_of_chars)++);
       if (str1 == NULL)
       {
         printf("\nERROR: realloc failed in read_line.\nTerminating.\n");
         exit(EXIT_FAILURE);
       }
     }
  }
  // at the end of the loop, str1 will contain the whole line
  // of input, except for the new-line char. '\n' will be stored in ch
  str1[i++] = ch;
  str1[i] = '\0';  // store the null char at the end of the string

  return str1;
}

int *create_array(char *str, int n)
{
  int *array, i, j, k, num_of_ints = 0;

  for (i = 0; i < n; i++)                         // computing number of numbers entered
    if (str[i] == ' ' || str[i] == '\n')
      num_of_ints++;

  array = calloc((size_t) num_of_ints, sizeof(int));    // allocacting necessary space for the array
  if (array == NULL)
  {
    printf("\nERROR: calloc failed in create_array.\nTerminating.\n");
    exit(EXIT_FAILURE);
  }

  k = 0;
  i = 1;                          // populating the array
  for (j = n - 1; j >= 0; j--)
  {
    switch (str[j])
    {
      case '0': case '1': case '2':
      case '3': case '4': case '5':
      case '6': case '7': case '8':
      case '9': array[k] += ((str[j] - '0') * i);
                i *= 10;
                break;
      case '-': array[k] = -array[k];     // added to support negative integers
      default:  i = 1;
                if (str[j] == ' ' && (str[j - 1] >= '0' && str[j - 1] <= '9'))
                     /* only increment k
                      *right before a new integer
                      */
                  k++;
                break;
    }                                                                      
  }
  // the loop works in this way:
  // it reads the str string from the end
  // if it finds a digit, it will try to extract it from the
  // string and store in array, by adding to one of the elements
  // of array the current char - ASCII for '0', so that it actually gets a digit,
  // times the position of that digit in the number,
  // constructing the number in base 10: units have 1, decimals 10, hundreds 100, and so on
  // when it finds a char that's not a digit, it must be a space, so it resets i
  // and increments k, to construct a new number in the next element of array

  size_of_array_to_be_sorted = num_of_ints;
  return array;
}

所有内容都是我自己写的,所以如果您认为我使用了一些糟糕的方法或幼稚的方法之类的,请告诉我,以便我能够更正它们。无论如何,我的问题是在每次调用 malloc、calloc 或 realloc 之后,我都有这些“尝试处理错误”if 语句。我有一台 Linux 机器和一台 Windows 机器。我在具有 4GB RAM 的 Linux 上编写程序。我写了它,用 gcc 编译,必须改变一些东西才能让它工作,而且它运行完美。我没有问题。然后我将它复制到 USB 驱动器上,并在我的 Windows 机器上用 mingw 编译它,它有 8GB 的​​内存。我运行它,如果我给它超过 3 个 2 位整数,它会显示 错误:realloc 在 read_line 中失败。 终止。 至少我知道“错误处理”if 语句有效,但为什么会发生这种情况?它是相同的代码,机器有两倍的 RAM,其中大部分是免费的,并且它在 Linux 上运行没有问题。 这是否意味着我的代码不可移植? 是我做的不对吗? 算法错了吗? 该程序是否非常非常低效? 很抱歉这个问题很长。 如果您想回答,谢谢。

最佳答案

有问题的行是:

str1 = realloc(str, (*num_of_chars)++);

其中 *num_of_charsstr 的当前大小。因为您正在使用 post-increment,为新分配传递的值与当前分配的值相同,所以您没有使 str 变大,但继续并表现得好像你有。

关于C: realloc 适用于 Linux,但不适用于 Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41349890/

相关文章:

c - 删除/销毁功能逻辑(结构/尝试)

c - 如何在 exec 期间处理 C shell 程序中的输入

mysql - Wordpress 延迟最多 2 分钟的页面响应时间

linux - 向现有 PDF 文件添加裁剪标记

c - 使用 scanf() 从键盘读取时从先前输入读取换行符

函数的常量指针参数

linux - `sort -k1` 和 `sort -k1,1` 之间的区别

c - 系统调用返回值和errno

python - USB串口数据发送乱码

c - 如何在 Windows 上用 C 编写 unicode hello world