-5

I'm pretty sure this this piece of code gives me an infinite loop, (I have left if for a very long time and nothing happens), and I've been starring at this for 2 days now and i don't have a clue why it keeps looping. Any ideas?

    int r = 0;
    int H = 0;
    int g = 0;

    while (r < (3265920)) {

        while (g < 79338) {
            //printf("middle");
            if (!strcmp(arr1[g], Arr2[r])) {

            strcpy(out[H], arr1[g]);
            H++;
        }
            g++;
        }

    r++;
    g = 0;
}

Q=0;
while (Q < 79338) {


    printf("%d: %s\n",Q, Ans[Q]);
    Q++;

}

All the arrays have the correct memory allocation outside the main.

static char arr2[NINE_FACT * 9][10];
char Ans[79339][10];
char arr1[79339][45];
Codor
  • 17,447
  • 9
  • 29
  • 56
J.doo
  • 11
  • 3

2 Answers2

2

Your inner loop is running 259111560960 times that will likely take a long time. What is H being used for? On a 32 bit machine H could cause bugs because it's going to overflow. Note, the word length of the machine you're on is important ie what does this code produce on your machine?

printf("sizeof(int) == %zu\n", sizeof(int));

I'm surprised it runs properly.

out[H]; //This array could be huge.

If this is a normal array and you're on a 64bit machine I'd expect a segmentation fault or and this can sometimes take some time a Core Dump.

Assuming a 32bit machine and single characters as strings out[H] will require 2^31 bytes * 2 due to null terminator ie just over 4 Gb of RAM?

Harry
  • 11,298
  • 1
  • 29
  • 43
  • Does int type can handle such value? – Joel Mar 24 '16 at 18:38
  • depends on the machine @Joel – lost_in_the_source Mar 24 '16 at 18:39
  • 1
    both r and g are being tested before they would overflow. The point is you are running strcmp and strcpy nearly 26 billion times. I don't know what "H" is being used for but if you are on a 32 bit machine it's going to overflow. – Harry Mar 24 '16 at 18:41
  • H is only incremented of two arrays are identical so it isn't added evrey time. – J.doo Mar 24 '16 at 19:18
  • it's checking all possible permutations, (inc. all lengths) of a random 9 letter string against all words so they will not match more than 80,000 times – J.doo Mar 24 '16 at 19:21
0

value of ris never going to be 3265920 if it is 2 byte of memory allocation

either you change the datatype of r to long or change the condition of r so that it will be in the range of int

first u analyze the range of int

Nutan
  • 778
  • 1
  • 8
  • 18