×

# maximum size of an array

 2 2 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 3★princerk 738●7●11●23 accept rate: 5% 1 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) 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★

 3 Have noticed something in SPOJ. I think codechef is similar to that so this will help. http://www.spoj.com/forum/viewtopic.php?f=41&t=9851 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 75●1●3●5 accept rate: 50% 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)
 9 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 fxn...it 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 4★kunal361 6.0k●13●32●72 accept rate: 21%
 3 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 3★kcahdog 10.0k●28●54●129 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 http://ideone.com/286OTE . 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★
 1 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 6★torque 427●1●1●11 accept rate: 17%

//here is my code

# include<vector>

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++)
{
if(v[i])
for(int j=i;j*i<MAX;j++)
v[j*i]=false;
}

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

3★princerk
73871123
accept rate: 5%

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) 3★

(10 Nov '13, 00:13) 3★
 0 @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 3★damn_me 2.6k●2●13●36 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)
 0 @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>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 3★damn_me 2.6k●2●13●36 accept rate: 24%
 0 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: #include using namespace std; bool prime[100001]; int k=0; void sieve(){ prime[1]=true; prime[0]=true; for(int i=2;i<=100000;i++){ if(prime[i]==false){ for(int j=i*i;j<=100000;j=j+i){ prime[j]=true; } } } } answered 20 Feb '16, 20:52 2★hv2906 5●2 accept rate: 0%
 0 Thanks! it helped answered 20 Feb '16, 22:52 2★hv2906 5●2 accept rate: 0%
 0 depends on your stack size in linux to increase your stack size to unlimited or a particular value use ulimit -s 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 1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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:

×1,916
×861
×43
×22

question asked: 09 Nov '13, 22:48

question was seen: 85,348 times

last updated: 10 Jul '17, 02:11