what is the maximum size of array that is permissible in c++?? can't we use an array of size 10^9 ..plz reply
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
what is the maximum size of array that is permissible in c++?? can't we use an array of size 10^9 ..plz reply
Название |
---|
In theory, the maximum possible array size is
std::numeric_limits<std::size_t>::max()
. The maximum possible array length is the above value divided by the size of the array's element.In practice, the maximum array size you can allocate is limited by available memory and OS restrictions.
In programming competitions, the maximum array size in bytes is limited by memory limit that is specified in the problem. Since most recent problems on Codeforces have a memory limit of 256M = 268435456 bytes, which is less than 109 bytes, you probably can't allocate such an array. Check the memory limit of your problem to be sure.
EDIT: I forgot to mention that if you want an array of
bool
s, then you can save space by packing 8bool
s¹ into each byte, which will reduce memory usage by 8 times. You can do it either using manual bit operations or withstd::bitset<>
(link).¹Formally,
CHAR_BIT
elements.Depends. for win 32 there are theoretical limit 4 GB (2^32 bytes) memory for all application. In practice there are 2 GB for user (programmer) and 2 GB for system (memory where loaded system dll's, file descriptors, etc). particular app can be tuned to use 3 GB for user, and 1 GB for system. For the 64-bit OS I don't know the limits, but i think they are very large, theoretically 2^64 bytes. In practice there are no hardware can operate 2^64 bytes of RAM.
A few days ago my friend has optimized memory usage of his application, when replaced dynamically allocated string class by ui32. His application works on the server powered by 64-bit unix, and uses 20-40 GB of memory. Data was divided into several blocks with the total size of string's of 2^32, and instead char* (8 bytes) was used ui32 (4 bytes) — 'pointer' in the array char[(size_t)1 << 32] where strings are placed one after another with terminating null character. The average size of used strings is less than a hundred, and because of dynamically allocated memory there was sufficient overhead. So, now it's normal to use array of size 2^32 :)
"10^9" means "O(n) algorithm is not allowed" in programming contest. So you should think the other way to solve the problem!
On codeforces, O (n), where n <= 10 ^ 9 passes tests with ~2 sec. On topcoder 4 * 10 ^ 9 passes within 2 seconds.
Are you saying about the Xor problem? The problem's fixed number is extremely small so in usual problem, I think O(n) solutions aren't allowed...
and in this problem, my acquaintance hacked many O(n) solutions... http://codeforces.net/contest/173/problem/A