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

Recommended Answers

All 15 Replies

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.

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

Thanks for the reply

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 for Mouche

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

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;

    }

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.

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.

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.

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

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

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 ass to be honest.

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

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.

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

Thanks

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.).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.