c - 段错误: 11 on mac but not on linux while creating an array in C

标签 c arrays linux macos quicksort

您好,我最近开始测试 QuickSort。我编写了一个程序,它根据用户输入的大小创建数组并用随机数填充它,然后使用快速排序对其进行排序。现在这是我的问题。在只有 4GB RAM 的 Linux 机器上,我可以在计算机变得不可用之前创建大小高达 10^8 的数组。在我的 8GB RAM 的 Mac 上,我只能创建一个大小最大为 10^6 的数组。如果我尝试创建一个大小为 10^7 或更大的数组,则会出现段错误。是操作系统的硬性限制吗,可以更改吗? 这是我的代码:

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

int FindPivot(int i, int j);
void PrintTable(int* table, int size);
void NextThing(int* table, int size);
void QuickSort(int* table, int i, int j);

void RandomizeTable (int* table, int size) { 

  int i;
  for (i = 0; i < size; i++) {
    table[i] = -10000 + rand() % (10000+1-(-10000));
    //printf("%d\t", table[i]);
  }
  printf("\n");
  NextThing(table, size);
}

void NextThing(int* table, int size) { 
  printf("Sorting the table...\n");
  clock_t x = clock();
  QuickSort(table, 0, size - 1);
  clock_t y= clock();
  printf("Time it took : %fs\n", ((double)(y - x))/CLOCKS_PER_SEC);

  //Second sorting of the table, just to see how long does it take for quicksort to sort an already sorted table
  printf("Sorting the table...\n");
  clock_t x2 = clock();
  QuickSort(table, 0, size - 1);
  clock_t y2= clock();
  printf("Time it took : %fs\n", ((double)(y2 - x2))/CLOCKS_PER_SEC);


  exit(0);
}

void Swap(int* table, int i, int j) {
  int temp;
  temp = table[i];
  table[i] = table[j];
  table[j] = temp;
}

int Partition(int* table, int i, int j) {
  int p, q, key; 

  p = FindPivot(i, j);
  key = table[p];

  Swap(table, i, p);

  for (p = i, q = i + 1; q <= j; q++)
    if (table[q] < key) {
      p++;
      Swap(table, p, q);
    }
    Swap(table, i, p);
  return p;
}

void QuickSort(int* table, int i, int j) {
  int p;
  if (i < j) {
    p = Partition(table, i, j);
    QuickSort(table, i, p - 1);
    QuickSort(table, p + 1, j);
  }
}//QuickSort 

void PrintTable(int* table, int size) {
    int i;
    for (i = 0; i < size; i++)
        printf("%d", table[i]);
    printf("\n");   
}

int FindPivot(int i, int j) {
  int pivot;
  /*pivot = i + rand() % (j + 1 - i); */ //Method I randomizing pivot
  pivot = (i + j) / 2; //Method II arithmetic avarage
  return pivot;
}



int main () {   

    time_t t;
    srand((unsigned) time(&t));

  int n;
  printf("Array size:");
  scanf("%d", &n);  
  int tab[n];     //Here is where error occurs if array size is > 10^6


  RandomizeTable(tab, n);

}

我几乎可以肯定制作这样大小的数组是有问题的。我尝试用 printf 调试代码。如果它是在创建数组之前(在 main() 中),它会打印文本,如果我把它放在后面,它不会打印它。

最佳答案

假设您使用的是 C99,它可能是特定于实现的(但可能不是),其中任何对象的最大大小受到 SIZE_MAX 的限制,从 this post ,这意味着最小值(理论上)可能小于 10^6 (1,000,000) 字节。

如果这是问题,您可以检查类似的内容

size_t max_size = (size_t)-1;

来自here

否则,另一篇文章是您最好的选择 - 如果您无法在堆栈上实现它,请使用 malloc可以在堆中分配它。

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

这将在堆中分配 (n * sizeof(int in bytes)) 字节,并且应该可以工作。

请注意,如果您以这种方式分配内存,则需要手动删除它,因此您应该调用 free完成后就可以了。

free(tab)

关于c - 段错误: 11 on mac but not on linux while creating an array in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54188388/

相关文章:

c - 结构中的指针中不允许使用 VLA 有充分的理由吗?

c++ - 在 Linsched 中使用 task_struct

arrays - 值未插入到数组中,数组返回空白值

linux - 无法在 Linux 上安装 libpcap

c - C语言中如何使用管道连接两个子进程

c - C 编程 K&R 练习 1-13

objective-c - 目标/C : Sorting an array on a custom order

python - 多维数组上的 numpy 高级索引

linux - 如何在 shell 脚本的不同行上显示 lsusb 结果?

linux - 我的 cpu 每秒有多少中断?