C 程序不在 Windows 上执行。它在 Unix 上

标签 c arrays algorithm sorting

我完成了给定的代码示例作为家庭作业,它在我的 64 位 OS X 设备(使用 gcc 编译)上运行良好。但它不会在我的 Windows 8 64 位机器上执行(使用 MinGW - gcc 编译)。我在两台机器上都使用了 gcc sort.c -Wall -Wextra。 不执行意味着程序在无限循环中停留在第 64 行。此外,我认识到这发生在 loop 在第 64 行达到 11 之后。

它也可以在键盘上运行 ( http://codepad.org/BoLhqtzv )。

我尝试使用不同的指针算法来访问数组,但都没有用。

程序对长度为 n 的数组 x 进行排序。在数组 x 中就像一个袋子。所以一个数字可以出现多次。函数 sort 获取数组 x 的长度、数组 x 上的指针和不同数字的计数(10:从 0 到 9)。诀窍在于数组 muh 知道哪个数字在数组 x 中出现的频率。这是可行的,因为 x 中的数字在 N(自然数)中是连续的。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define M 10

/* This function generates an array of random integers in the range [0,M-1] of length n. */
int* random_array(const int n) {
  int *x;
  int i;

  x = (int*) malloc(n * sizeof(int));

  srand(time(NULL ));

  for (i = 0; i < n; i++) {
    x[i] = rand() % M;
  }

  return x;
}

/* print an array. */
void print_array(const int n, const int *x) {
  int i;

  printf("array: ");
  for (i = 0; i < n && i < 32; i++) {
    printf("%d ", x[i]);
  }
  if (n > 32) {
    printf("...");
  }
  printf("\n");
}

/* check if a given array is sorted in ascending order. */
void is_sorted(const int n, const int *x) {
  int i;

  for (i = 1; i < n; i++) {
    if (x[i - 1] > x[i]) {
      fprintf(stderr, "ERROR: Array is not sorted!\n");
      return;
    }
  }
  printf("Array is sorted!\n");
}

/* n is the length of the array x and m is the same m as on your exercise sheet.
 * In this case m is set to 10. */
void sort(const int n, int *x, int m) {
  /* allocates memory for an zero initialized array */
    int *muh = (int*) calloc(m, sizeof(int));
    int loop; //count variable

    /*counts each number in the array*/
    for(loop = 0; loop < n; loop++){
        muh[x[loop]]++;
    }

    /*Overrides x, Each number appears muh[loop] times at the beginning of line 65*/
    for(loop = 0; loop < n; loop++){
        for(; muh[loop] > 0; muh[loop]--) {
            *(x++) = loop;
       }
    }
}

int main() {
  int *x;
  int n;

  /* set length of the arrays */
  n = 1 << 5;

  /* get a random integer array of length n */
  x = random_array(n);

  /* print the unsorted array */
  print_array(n, x);

  printf("\n");
  printf("sorted array:\n");

  /* sort x by using sort, check if it is sorted and print it out */
  sort(n, x, M);
  is_sorted(n, x);
  print_array(n, x);

  return 0;
}

最佳答案

您的 muhm 元素的空间,但是

for(loop = 0; loop < n; loop++){
    for(; muh[loop] > 0; muh[loop]--) {
        *(x++) = loop;
   }
}

你循环遍历 n。如果 n > m,您有未定义的行为。

 * In this case m is set to 10. */

n = 1 << 5;

表示它咬你。

它似乎适用于某些系统但不适用于其他系统,好吧,未定义的行为会为您做到这一点。

关于C 程序不在 Windows 上执行。它在 Unix 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16238261/

相关文章:

php - 如何生成 Y-m-d 格式的工作日数组?

performance - 用于跟踪过去 X 小时数据的数据结构

javascript - 如何线性插值 (lerp) 范围输入到范围输出?

algorithm - 最大流量和一些条件

c - scanf 使用空格作为行尾

c - const-casting 是通过 union 未定义的行为吗?

c - 如何调整使用 wglCreateContext 创建的 openGL 窗口的大小?

c - 如何在 C 中使用 for - while 循环制作领结形状

php - 如何从数据库中显示两次行信息

javascript - 将对象数组转换为属性数组