【问题标题】:C Programm does not execute on Windows. It does on UnixC 程序不能在 Windows 上执行。它在 Unix 上
【发布时间】:2013-04-26 13:55:08
【问题描述】:

我完成了一个给定的代码示例作为家庭作业,它在我的 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;
}

【问题讨论】:

  • 定义“不会执行”——错误信息?什么都没有?
  • PS:[不要在 C 中强制转换 malloc] -> stackoverflow.com/questions/605845/…
  • 刚刚添加了解释。
  • 我们的科学计算教授以这种形式介绍了 malloc。但是,我对 C 编程了解/理解细节并没有那么远,但它可能没有那么危险,或者?
  • 向你的教授展示我提供的链接,看看他要说什么。正如链接所指出的那样,它本质上是危险的。

标签: c arrays algorithm sorting


【解决方案1】:

您的muh 有空间容纳m 元素,但是

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

你遍历n。如果n &gt; m,你有未定义的行为。

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

n = 1 << 5;

表明它咬你。

它似乎在某些系统上有效,但在其他系统上无效,嗯,未定义的行为可以为您做到这一点。

【讨论】:

  • m 是 n 中不同数字的计数。如果 n = {0, 0, 0, 1, 1, 2} 那么 muh ={3, 2, 1} 和 m = 3
  • @froehli 好吧,我不知道为什么会这样,但是 IIRC,MinGW 使用 Windows C 库,并且它的 malloc 可能与 glibc 的行为不同,所以当你走出分配的块,在 Linux 上你只是覆盖了一些未使用的内存,而在 Windows 上,你覆盖了一些重要的东西。但要确定的是,需要在一个好的调试器下运行它——也许这还不够。
【解决方案2】:

sort(n, x, M); -> sort(n, x, n);

loop &lt; n 和 muh[0]..muh[9] (int *muh = (int*) calloc(m, sizeof(int)))

muh[loop] 在循环 >= m 时超出范围

【讨论】:

  • 使用 sort(n, x, M) 正确调用了 sort 函数,但我在循环条件中使用了错误的变量。正确版本:codepad.org/lpAajc4v
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多