分配大小非常大的数组

发布于 2024-10-10 15:40:33 字数 475 浏览 2 评论 0原文

如何创建一个非常大的数组?那么我无法创建一个大小为 INT_MAX 的数组..如何实现这一点。?

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define SIZE 2147483647

int main() {    
   int *array;    
   unsigned int i;

   array = malloc(sizeof(int) * SIZE);    
   if(array == NULL)    {
      fprintf(stderr, "Could not allocate that much memory");
      return 1;    }

   for(i=0; i<1; i++)    {
      array[0] = 0;    
   }    

   free(array); 
}

How can create an array of size very large?? Well i am not able to create an array of size INT_MAX.. how could be achieve this.?

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define SIZE 2147483647

int main() {    
   int *array;    
   unsigned int i;

   array = malloc(sizeof(int) * SIZE);    
   if(array == NULL)    {
      fprintf(stderr, "Could not allocate that much memory");
      return 1;    }

   for(i=0; i<1; i++)    {
      array[0] = 0;    
   }    

   free(array); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天生の放荡 2024-10-17 15:40:33

您几乎肯定会达到平台限制。如果你只有 32 位地址空间,那么 4G 就足够了。实际上,它会少得多,因为部分地址空间将被其他东西占用。

对于 64 位地址空间,这可能是可能的,但是一旦达到该分配级别,您应该问自己是否确实有必要。

解决该问题的一种方法是使用内存不足的存储(例如磁盘),只将需要的内容放入内存。

换句话说,将数据结构分割成(例如)1M 的块并一次处理 1M。

您可以使用多种缓存算法来有效地完成此操作,具体取决于数据结构的使用模式。

例如,对于真正的顺序访问,您可以一次在内存中拥有一个块。对于真正的随机访问,在缓存场景中,您可能希望一次在内存中拥有多个块 - 每个内存结构都存储 1M 数据及其在内存外存储中的位置,因此您可以使用 LRU 算法以及脏数据的写回等等。

You're almost certainly hitting a platform limit. If you only have a 32-bit address space, 4G is as much as you can hope to address. In reality, it will be much less since part of the address space will be taken up by other things.

With a 64-bit address space, it may be possible but, once you get to that level of allocation, you should ask yourself if it's actually necessary.

A way of solving the problem is to use out-of-memory storage such as disk and only bring into memory what's needed.

In other words, segment the data structure into (for example) 1M chunks and process it 1M at a time.

There are plenty of caching algorithms you can use to do this efficiently depending on the usage patterns of your data structure.

For example, for truly sequential access, you could have one chunk in memory at a time. For truly random access, you may want to have several chunks in memory at a time in a cache scenario - each in-memory structure stores both the 1M of data and it's location in the out-of-memory storage so you can use LRU algorithms and write-back of dirty data, and so forth.

孤千羽 2024-10-17 15:40:33

您的第一个问题不是分配本身,而是看似简单的表达式 sizeof(int) * SIZE。如果 int 有 4 个字节,则该操作的结果是 0x1FFFFFFFC。这需要33位来表示。如果您的平台只有 32 位的 size_t 类型,则乘法结果会回绕(size_t 无符号)并给出 0xFFFFFFC

如果您在调用 malloc 时刚刚使用了上面的 33 位值,您的编译器可能会告诉您该数字无法表示。

Your first problem is not the allocation itself but the seemingly simple expression sizeof(int) * SIZE. The result of that operation is 0x1FFFFFFFC if int has 4 bytes. This needs 33 bits to be represented. If your platform only has a size_t type of 32 bit the result of the multiplication wraps around (size_t is unsigned) and gives you 0xFFFFFFFC.

If you'd had just used the 33 bit value above in your call to malloc, your compiler would probably have told you that that number isn't representable.

掩饰不了的爱 2024-10-17 15:40:33

您正在创建一个最小大小为 4GB 的数组。你确定你有那么多空闲内存吗?

You are creating an array of minimum 4GB size. Are you sure you have that much free memory?

各自安好 2024-10-17 15:40:33

到目前为止,每个人都在尝试回答您提出的问题,但我想采取另一种方法。根据我的经验,您很少真正需要您尝试分配的类型/大小的数组。通常还有其他方法可以完成您需要的操作,而无需创建如此庞大的结构(解析数组、堆栈、队列、映射)。

我很好奇你想用这个数组做什么。 。 。我敢打赌,如果我们理解您想要解决的问题,您实际上并不需要它。

另一方面,如果这是一项智力练习(比如我可以分配多少资金),也有一些方法可以回答此类问题。

如果你想玩的话。 。 。你真正想要实现的目标是什么?

So far everyone is trying to answer your question as asked, but I'd like to take another approach. In my experience, it is rare that you actually need an array of the type/size you are trying to allocation. There are often other ways of doing what you need without creating such a huge structure (parse arrays, stacks, queues, maps).

I'm curious what you want to DO with this array . . . I'm betting you don't actually need it if we understood what problem you were trying to solve.

On the other hand, if this is an intellectual exercise (like how big can I allocate), there are approaches to answering those kinds of questions as well.

If you want to play . . . what is it your are actually trying to accomplish?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文