开发者

Allocation of array of size very large

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); 
}


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.


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.


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜