views:

73

answers:

3

I need to create an array as big as possible on my 32bit Linux computer. What is the biggest array size that my computer can handle? Is it 2^32 ?

A: 

http://stackoverflow.com/questions/216259/is-there-a-max-array-length-limit-in-c

There's 2 limits, the physical memory, but ignoring that, it should be (2^32)-1, given that nothing else at all is going on, as in the same way that you're limited by physical memory, your program is limited to what it's allowed to take too.

AaronM
And where exactly the program code will be stored in this case (2^32-1)?
SigTerm
+2  A: 

Also, if we assume the figure is about 3GB (which seems reasonable), then you also have to remember that integers are 4 bytes each: giving you a maximum of 3G/4 = around 750 million integers.

And of course, there's also the fact that if you've allocated 3GB worth of array, then you won't have any address space left for anything else!

My personal recommendation is, if you're planning to work with large arrays of numbers, to use memory-mapped files (on Linux this is done with mmap) and only map a small section of the file into memory at a time (say, 100MB). That will mean you can handle any sized data set you like, and you won't exhaust your address space.

Dean Harding
+3  A: 

I assume it will be somewhere somewhere 2^31 and 2^32, not more, probably even less (part of address space will be taken by libraries and application code). One process cannot access more than 2^32 memory, and some OS mark half of address space for special purposes. Not sure if linux does this or not.

Size of pointer is 32bit, so you can't get a linear chunk of memory larger than 2^32, no matter what you do. Anything that is not a "linear chunk of memory" can't be called an array.

SigTerm
This answer is not actually very useful -- while it is true that you cannot allocate a block of memory bigger than 2^32 given a 32-bit pointer, you completely fail to account for the size of the integers themselves, as codeka has in his answer. As he's shown, the real limit is more likely around 750 million integers, although even that is probably too large as it leaves no room for the program code, libraries, etc. As others have said, there is a more pressing question: why is user69514 actually trying to do?
Eric Melski
@Eric Melski:1) I was talking about size of block in bytes. 2) The reason for big array is not important. If OP tries to do something "wrong", then he'll eventually discover mistake and learn something useful from it.
SigTerm