array containing 20 crores elements
Hi, can anyone please tell me how to create a array of 20 crore (200000000) elements.
I tried, but getting runtime error, might be that much memory is not available.
Thanks
Related Article: array total problem
is a C discussion thread by on93 that has 4 replies and was last updated 10 months ago.
ram619
Junior Poster in Training
76 posts since Mar 2010
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
A crore == 10M, right? lahk == 10K as I recall... :-)
An array of 200M elements (assuming array of pointers pointers) on a 64-bit machine will take 200Mx8 bytes (minimum) of memory, which is 1.6GB. Most systems won't allow you to allocate such sizes on the stack, so you may need to allocate it on the heap with calloc/malloc calls. On some systems, you can allow users to allocate huge stacks, but it really isn't a good idea.
So, can you provide some indication of what you are trying to accomplish? We may be able to provide more relevant advice in how to manage that much memory if we knew.
rubberman
Posting Maven
2,696 posts since Mar 2010
Reputation Points: 378
Solved Threads: 316
Skill Endorsements: 53
Just trying to check my coding efficiency :) trying to check how long it would take to traverse that much.
Thanks for the reply
ram619
Junior Poster in Training
76 posts since Mar 2010
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
I tried doing it with malloc() but still same, still runtime issue. Even its not even happening for 20lacs.
I am using "Microsoft Visual C++ 6.0" IDE
ram619
Junior Poster in Training
76 posts since Mar 2010
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
Hi, now the runtime error is resolved.
Now i have different quesion, the below given code takes around .665 seconds to execute. I want to reduce the time to .3 sec.
Please tell me how to optimise.
and one more thing I haven't used calloc() to save time, junk values would work for me.
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned char *p=NULL;
unsigned char *p1;
int i=0,k=1,val;
p=malloc(200000000);
p1=p;
/* for(i=0;i<200000000;i++)
{
*p1=k;
k++;
p1++;
if(k==254)
{
k=0;
}
}
p1=p;
*/
// printf("enter the value\n");
val=250;
for(i=0;i<200000000;i++)
{
if(*p1==val)
{
printf("value dup\n");
return 0;
}
else
{
p1++;
continue;
}
}
return 0;
}
ram619
Junior Poster in Training
76 posts since Mar 2010
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
Please tell me how to optimise.
Delete the printf() line. I/O constitutes some of the most, if not the most time intensive operations.
On a side note, your code exhibits undefined behavior by accessing uninitialized memory. There's no guarantee that the program won't crash intermittently.
deceptikon
Challenge Accepted
3,505 posts since Jan 2012
Reputation Points: 824
Solved Threads: 481
Skill Endorsements: 58
Thanks for the reply deceptikon.
I already commented printf(), lets use calloc() instead of malloc(). But the thing is program takes .685 seconds to complete and I want to reduce the time to .3 seconds.
ram619
Junior Poster in Training
76 posts since Mar 2010
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
lets use calloc() instead of malloc()
Okay, but that's not going to improve matters. If anything it'll make the code slower because on top of allocating memory (that's another extremely expensive operation), calloc() has to initialize it.
But the thing is program takes .685 seconds to complete and I want to reduce the time to .3 seconds.
I assume you're measuring using a profiler? Why have you picked .3 seconds as the end all be all of performance on this seemingly pointless program? What makes you think .6 seconds isn't fast enough? All of this seems very arbitrary to me, so I'm wondering what you're actually trying to accomplish.
deceptikon
Challenge Accepted
3,505 posts since Jan 2012
Reputation Points: 824
Solved Threads: 481
Skill Endorsements: 58
Hi, someone challenged me, that the execution of this program should not take more then .3 seconds. Thats why I am bent upon :)
ram619
Junior Poster in Training
76 posts since Mar 2010
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
I am using code blocks, that diaplays time spent in execution, on its own.
ram619
Junior Poster in Training
76 posts since Mar 2010
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
Hi, someone challenged me, that the execution of this program should not take more then .3 seconds.
Okay, so what is the purpose of this program, and who is this person to tell you how long it should be taking? Sounds like someone talking out of their ### to be honest.
deceptikon
Challenge Accepted
3,505 posts since Jan 2012
Reputation Points: 824
Solved Threads: 481
Skill Endorsements: 58
lol, he is working in some company and he told me that its a problem from codility.com
ram619
Junior Poster in Training
76 posts since Mar 2010
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
You might want to get all of the details for the problem then. Unless he sent you an exact copy paste from the problem, you're getting a paraphrased version that may be leaving out very important information.
deceptikon
Challenge Accepted
3,505 posts since Jan 2012
Reputation Points: 824
Solved Threads: 481
Skill Endorsements: 58
Ok, I will ask him complete question then, I'll tell you.
Thanks
ram619
Junior Poster in Training
76 posts since Mar 2010
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
I don't fully understand the problem, but it sounds quite interesting. I went to codility.com, but they don't have their problems listed like some of the code challenge sites (Euler, Code Chef, SPOJ, etc.).
Adak
Posting Virtuoso
1,642 posts since Jun 2008
Reputation Points: 456
Solved Threads: 196
Skill Endorsements: 7