You are not logged in. Please login at to post your questions!


maximum size of an array


can anyone tell me what is the maximum size of different types(bool,int,long long ...etc ) of array (or vector) we can take in c++

asked 09 Nov '13, 22:48

princerk's gravatar image

accept rate: 5%

wikified 10 Nov '13, 09:36


I have tried with 10^6 and it works.. but for 10^7 it isn't.. so it lies something in between. Waiting for someone to answer this. :D

(09 Nov '13, 22:59) koders17061★

i dont know why my program stops working with size 10^5 even if with bool arry

(09 Nov '13, 23:07) princerk3★

@princerk...can u give ur code where this happens!!!

(09 Nov '13, 23:09) kunal3614★

Have noticed something in SPOJ. I think codechef is similar to that so this will help.

As I have already mentioned, its something from 10^6 to 10^7 depending on the stack size.

As mentioned on codechef wiki,

We use the SPOJ online judge as our backened.

It is therefore clear that the stack size is 256MB.


answered 09 Nov '13, 23:16

koders1706's gravatar image

accept rate: 50%

edited 09 Nov '13, 23:21

but when i run the code given it stops working im using devcpp...can i increase the size of an array explicitly

(09 Nov '13, 23:27) princerk3★

@princerk i dont think you would be able to increase the stack size of those in codechef.. Try some other optimization techniques..

(12 Nov '13, 19:04) koders17061★

array size 10^7 to 10^8 declared globally(i.e. on heap) is possible..if u declare nething in the main or in ne goes into the stack which has a smaller size hence ur 10^7 array did not work out..try declaring it globally and it should work..this is what i have observed...hope this helps..:)


answered 09 Nov '13, 23:09

kunal361's gravatar image

accept rate: 21%

Code chef runs on SPOJ server's. SPOJ has 2 server clusters on which submissions are run , 1 old 733 Mhz cluster which has a memory limit of 256 mb and a newer cluster of 3Ghz with a memory limit of 1.5 gb.

Most practice problems run on the older cluster of limit 256mb and array size limit will most probably be around 6 * 10^7 which occupies around 230-240mb of memory. i have tried [10^5][600]and it has worked for me. Most of the current contests run on the newer cluster of size 1.5 gb but limits for maximum size of a single array is still around 10^7 to 10^8 as it is very difficult to allot contiguous memory locations . However as total memory limit is around 1.5 gb , you may be able to declare around multiple (5 or 6) arrays of this length .

If you need that much memory ,my suggestion is that you declare multiple arrays of size around 6*10^6 for contest problems.


answered 09 Nov '13, 23:35

kcahdog's gravatar image

accept rate: 14%

tnx @kcahdog ...can u tell me why the above code giving runtime error on ideone when i decrease MAX to 10^4 it runs.

(10 Nov '13, 00:03) princerk3★

I do not know why that code gave an error. However i declared that array as local and it ran fine. Here is the link .

My best guess is ideone restricts the amount of global memory accessible to users while the full 256 mb can be used for local variables/arrays. This might be done to easily manage each submission locally as it will prevent everyone from using too much common memory.

There is nothing wrong with the code. I guess it's just because the ideone is designed like this.

(10 Nov '13, 02:37) kcahdog3★

finally i found out the bug..actully this is the int overflow.when i take MAX 10^6 (i*j overflow) when i chage 'i' and 'j' to long long it runs without error. tnx a lot @kcahdog

(10 Nov '13, 09:36) princerk3★

Its beacuse of your

for(int j=i*i;j<=100000;j++) loop

Here your i*i goes out of int bounds and a overflow occurs. Try taking both i and j as long int and it should work fine.


answered 20 Feb '16, 21:55

torque's gravatar image

accept rate: 17%

//here is my code



using namespace std;

define MAX 100000

bool v[MAX]; void pre() {

for(int i=2;i<MAX;i++)v[i]=true;
for(int i=2;i<MAX;i++)
    for(int j=i;j*i<MAX;j++)

} int main() { pre(); int i; cin>>i; cout<<i; return 0; }

check this link


answered 09 Nov '13, 23:19

princerk's gravatar image

accept rate: 5%

edited 10 Nov '13, 00:12

Could you give a link to your code as it would be better to understand. Also what error are you getting?

(09 Nov '13, 23:37) kcahdog3★

i have given the link

(10 Nov '13, 00:13) princerk3★

@all Usually 1-D array of size 10^6 or 7 works fine but what if i want to allocate a 2-d array whose max size can be upto [10^6][10^6]. That aint gonna work probbaly.. so if i allocate two 1-D arrays of size 10^6, they work fine.. However the space allocated in memory for both are same i suppose if not allocated dynamically. Why such happens?


answered 13 Dec '13, 23:54

damn_me's gravatar image

accept rate: 24%

@kcahdog @princerk Can you give example of any such code that clears my above doubt.

(13 Dec '13, 23:56) damn_me3★

@damn_me when u allocate an array of size [10^6][10^6] it will take (10^6)*(10^6) i.e 10^12 which is very much greater then two 1-D arrays of size [10^6] i.e 2 * (10^6) i think it clears ur doubt

(14 Dec '13, 00:58) princerk3★

So as all codechef problems have input data in the order of 10^8, array can't be used to store input data.So what are the other strategies?I am not able to solve problems bcoz of this and i am stuck from 2 weeks or so.Please help....

(14 Dec '13, 10:00) anonymousins2★

@anonymousins Usually if you are taking the array for i/p and that doesnt require manipulation, simply take a variable of the specified datatype and overwrite it the number of times required. I mean this .

for(i=0;i<n;i++) cin>>ele;

You could have taken an array for ele as int ele[n], but the above is much better. Also, there are many more strategies to do,but that depends on the type of question it is.


answered 14 Dec '13, 12:50

damn_me's gravatar image

accept rate: 24%

I am getting the same error on allocating array of size 100001. It shows runtime error on codechef and program stops working in devc++ Here's snippet of the code:

using namespace std;
bool prime[100001];
int k=0;
void sieve(){
    for(int i=2;i<=100000;i++){
            for(int j=i*i;j<=100000;j=j+i){

answered 20 Feb '16, 20:52

hv2906's gravatar image

accept rate: 0%

Thanks! it helped


answered 20 Feb '16, 22:52

hv2906's gravatar image

accept rate: 0%

depends on your stack size in linux to increase your stack size to unlimited or a particular value use ulimit -s <value> then it will work make sure value is > what ever array space you require plus your auto variables plus return frames etc blah ...


answered 10 Jul '17, 02:11

rajusolomon's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 09 Nov '13, 22:48

question was seen: 85,348 times

last updated: 10 Jul '17, 02:11