简体   繁体   English

使用C的最大大小数组程序?

[英]Maximum size array program in C?

with the following code, I am trying to make an array of numbers and then sorting them. 使用以下代码,我试图制作一个数字数组,然后对它们进行排序。 But if I set a high arraysize (MAX), the program stops at the last 'randomly' generated number and does not continue to the sorting at all. 但是,如果我设置了较高的arraysize(MAX),则程序将在最后一个“随机”生成的数字处停止,并且根本不会继续进行排序。 Could anyone please give me a hand with this? 有人可以帮我吗?

#include <stdio.h>

#define MAX 2000000

int a[MAX];
int rand_seed=10;

/* from K&R
   - returns random number between 0 and 62000.*/
int rand();
int bubble_sort();

int main()
{
    int i;

    /* fill array */
    for (i=0; i < MAX; i++)
    {
        a[i]=rand();
        printf(">%d= %d\n", i, a[i]);
    }

    bubble_sort();

/* print sorted array */
printf("--------------------\n");
for (i=0; i < MAX; i++)
printf("%d\n",a[i]);

    return 0;
}

int rand()
{
    rand_seed = rand_seed * 1103515245 +12345;
    return (unsigned int)(rand_seed / 65536) % 62000;
}

int bubble_sort(void)
{
    int t, x, y;
    /* bubble sort the array */
    for (x=0; x < MAX-1; x++)
        for (y=0; y < MAX-x-1; y++)
            if (a[y] > a[y+1])
            {
                t=a[y];
                a[y]=a[y+1];
                a[y+1]=t;
            }
     return 0;
}

The problem is that you are storing the array in global section, C doesn't give any guarantee about the maximum size of global section it can support, this is a function of OS, arch compiler. 问题是您将数组存储在全局节中,C不能保证它可以支持的全局节的最大大小,这是OS和arch编译器的功能。
So instead of creating a global array, create a global C pointer, allocated a large chunk using malloc. 因此,与其创建一个全局数组,不创建一个全局C指针,并使用malloc分配了一个大块。 Now memory is saved in the heap which is much bigger and can grow at runtime. 现在,内存被保存在更大的堆中,并且可以在运行时增长。

Your array will land in BSS section for static vars. 您的阵列将进入静态变量的BSS部分 It will not be part of an image but program loader will allocate required space and fill it with zeros before your program starts 'real' execution. 它不会成为映像的一部分,但是程序加载器将分配所需的空间,并在程序开始“实际”执行之前用零填充。 You can even control this process if using embedded compiler and fill your static data with anything you like. 如果使用嵌入式编译器,甚至可以用任何您喜欢的内容填充静态数据,您甚至可以控制此过程。 This array may occupy 2GB or your RAM and yet your exe file may be few kilobytes. 该阵列可能占用2GB或您的RAM,但是您的exe文件可能只有几千字节。 I've just managed to use over 2GB array this way and my exe was 34KB. 我刚刚设法以这种方式使用了超过2GB的阵列,而我的exe为34KB。 I can believe a compiler may warn you when you approach maybe 2 31 -1 elements (if your int is 32bit) but static arrays with 2m elements are not a problem nowadays (unless it is embedded system but I bet it is not). 我可以相信,当您处理大约2 31 -1个元素(如果您的int是32bit)时,编译器可能会警告您,但是如今带有2m个元素的静态数组已不是问题(除非它是嵌入式系统,但我敢打赌这不是问题)。

The problem might be that your bubble sort has 2 nested loops (as all bubble sorts) so trying to sort this array - having 2m elements - causes the program to loop 2*10 12 times ( arithmetic sequence ): 问题可能是您的冒泡排序有2个嵌套循环(与所有冒泡排序一样),因此尝试对具有2m个元素的数组进行排序会导致程序循环2 * 10 12次( 算术序列 ):

inner loop: 内循环:

1: 1999999 times 1:1999999次

2: 1999998 times 2:1999998次

... ...

2000000: 1 time 2000000:1次

So you must swap elements 所以你必须交换元素

2000000 * (1999999+1) / 2 = (4 / 2) * 1000000 2 = 2*10 12 times 2000000 *(1999999 + 1)/ 2 =(4/2)* 1000000 2 = 2 * 10 12

(correct me if I am wrong above) (如果我错了,请纠正我)

Your program simply remains too long in sort routine and you are not even aware of that. 您的程序在排序例程中只是停留太长时间,您甚至没有意识到。 What you see it just last rand number printed and program not responding. 您看到的只是打印的最后一个rand数字,程序没有响应。 Even on my really fast PC with 200K array it took around 1minute to sort it this way. 即使在我的具有200K阵列的非常快的PC上,也需要大约1分钟的时间来进行这种排序。

It is not related to your os, compiler, heaps etc. Your program is just stuck as your loop executes 2*10 12 times if you have 2m elements. 它与您的os,编译器,堆等无关。如果您有2m个元素,则循环将执行2 * 10 12次,因此程序只是卡住了。

To verify my words print "sort started" before sorting and "sort finished" after that. 为了验证我的文字,请在排序之前先打印“排序开始”,然后再打印“排序完成”。 I bet the last thing you'll see is "sort started". 我敢打赌,您会看到的最后一件事是“排序开始”。 In addition you may print current x value before your inner loop in bubble_sort - you'll see that it is working. 另外,您可以在bubble_sort的内部循环之前打印当前的x值-您会看到它正在工作。

The original C standard (ANSI 1989/ISO 1990) required that a compiler successfully translate at least one program containing at least one example of a set of environmental limits. 原始的C标准(ANSI 1989 / ISO 1990)要求编译器成功转换至少一个包含至少一组环境限制示例的程序。 One of those limits was being able to create an object of at least 32,767 bytes. 这些限制之一是能够创建至少32,767字节的对象。

This minimum limit was raised in the 1999 update to the C standard to be at least 65,535 bytes. 在1999年对C标准的更新中,此最低限制提高到至少65,535字节。

No C implementation is required to provide for objects greater than that size, which means that they don't need to allow for an array of ints greater than 不需要C实现来提供大于该大小的对象,这意味着它们不需要允许一个大于

(int)(65535 / sizeof(int)).

In very practical terms, on modern computers, it is not possible to say in advance how large an array can be created. 用非常实际的话说,在现代计算机上,不可能预先说出可以创建多大的阵列。 It can depend on things like the amount of physical memory installed in the computer, the amount of virtual memory provided by the OS, the number of other tasks, drivers, and programs already running and how much memory that are using. 它可能取决于诸如计算机中安装的物理内存量,操作系统提供的虚拟内存量,已运行的其他任务,驱动程序和程序的数量以及正在使用的内存量之类的内容。 So your program may be able to use more or less memory running today than it could use yesterday or it will be able to use tomorrow. 因此,您的程序今天使用的内存可能比昨天使用的内存更多或更少,或者明天使用。

Many platforms place their strictest limits on automatic objects, that is those defined inside of a function without the use of the 'static' keyword. 许多平台对自动对象(即在函数内部定义的对象)使用了最严格的限制,而没有使用'static'关键字。 On some platforms you can create larger arrays if they are static or by dynamic allocation. 在某些平台上,如果它们是静态的或通过动态分配,则可以创建更大的数组。

Dynamic Array 动态阵列

int *Array;
Array= malloc (sizeof(int) * Size);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM