1,105,169 Community Members

array containing 20 crores elements

Member Avatar
ram619
Junior Poster
100 posts since Mar 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
rubberman
Senior Poster
3,989 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 500 [?]
Skill Endorsements: 87 [?]
 
2
 

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.

Member Avatar
ram619
Junior Poster
100 posts since Mar 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Just trying to check my coding efficiency :) trying to check how long it would take to traverse that much.

Thanks for the reply

Member Avatar
ram619
Junior Poster
100 posts since Mar 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
Mouche
Posting Whiz in Training
291 posts since Oct 2006
Reputation Points: 63 [?]
Q&As Helped to Solve: 53 [?]
Skill Endorsements: 3 [?]
Featured
 
0
 

What is the runtime error? Can you show us the snippet of code where you're working with the array?

Member Avatar
ram619
Junior Poster
100 posts since Mar 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;

    }
Member Avatar
deceptikon
Eternally Awesome
4,675 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 681 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
1
 

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.

Member Avatar
ram619
Junior Poster
100 posts since Mar 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
deceptikon
Eternally Awesome
4,675 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 681 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
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.

Member Avatar
ram619
Junior Poster
100 posts since Mar 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, someone challenged me, that the execution of this program should not take more then .3 seconds. Thats why I am bent upon :)

Member Avatar
ram619
Junior Poster
100 posts since Mar 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am using code blocks, that diaplays time spent in execution, on its own.

Member Avatar
deceptikon
Eternally Awesome
4,675 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 681 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
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.

Member Avatar
ram619
Junior Poster
100 posts since Mar 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

lol, he is working in some company and he told me that its a problem from codility.com

Member Avatar
deceptikon
Eternally Awesome
4,675 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 681 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
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.

Member Avatar
ram619
Junior Poster
100 posts since Mar 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok, I will ask him complete question then, I'll tell you.

Thanks

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
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.).

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article