0

Possible Duplicate:
Max Array Size in C

My question is: Does Code::blocks have a max number of iterations for a loop?

I am running a monte carlo and I would like to run a million particles through a for loop. But the max it seems to go without crashing is 110000.

Thanks!

Some more info:

I am using a random number generator seeded by time:

    srand(time(NULL));

I then want to create a million particles (random)

    for(k=0; k<M; k++){
    R[k] = rand()*(1)/(double)RAND_MAX;
    z[k] = -log(1 - R[k])/(1000*U);

where M = Num/10 (I want to #define N 1000000)

This is the only thing I can think of that is creating the problem?

Here is an example code that will not work.

    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    #include <stdlib.h>

    int main(){

      srand(time(NULL));

       int j=0;
       int i;
       double A[1000000];

       for(i=0;i<1000000;i++){
          A[i] = rand()*(1.0)/(double)RAND_MAX;
           if (A[i] == 1.0){
              printf("Wow!\n");
           }
       }

       return 0;

    }

Could this be due to my settings of Code::blocks by any chance?

Community
  • 1
  • 1
Shinobii
  • 2,401
  • 5
  • 24
  • 39
  • 1
    There is no max number of iterations. You should give us the error message as well as the relevant code. –  Nov 22 '12 at 16:44
  • Unfortunately I do not get an error. I just get a popup saying Code::blocks has stopped working. – Shinobii Nov 22 '12 at 16:56
  • Could be a problem with the argument of log being 0. You should try Valgrind to make absolutely sure. – 1'' Nov 22 '12 at 17:13
  • 127219 Is the max number I can go prior to crashing. – Shinobii Nov 22 '12 at 17:16
  • Yep, your right about the log. But I still cannot go beyond the 127219... – Shinobii Nov 22 '12 at 17:20
  • Maybe it's just because you have an 8 megabyte array on your stack. Try replacing `double A[1000000];` with `std::vector A;`, and `A[i] = rand...` with `A.push_back(rand...);` – Useless Nov 22 '12 at 18:00

2 Answers2

2

There's no max number of iterations, but there is a limit on stack size.

When you declare a local variable, the memory is allocated from the stack. The size of A is too big to fit in the stack. (e.g. typical stack size on Windows is 1MB, 1000000 * sizeof(double) is far bigger.)

You can try changing A to a global variable or allocate it by malloc.

timothyqiu
  • 1,072
  • 11
  • 23
1

Edit: this is because the default maximum stack size is 1MB on Windows (but 8MB on Linux).

I don't have a final answer, but I do know:

a) It runs fine from a Linux command-line.

b) Valgrind gives tons of errors of this form:

==2465== Invalid write of size 8
==2465==    at 0x804851D: main (test2.c:16)
==2465==  Address 0xbe4480e4 is on thread 1's stack
==2465== 
==2465== Invalid write of size 4
==2465==    at 0x8048521: main (test2.c:16)
==2465==  Address 0xbe4480e0 is on thread 1's stack
==2465== 
==2465== Invalid read of size 4
==2465==    at 0x4081D69: printf (printf.c:35)
==2465==    by 0x8048528: main (test2.c:16)
==2465==  Address 0xbe4480e0 is on thread 1's stack
==2465== 
==2465== Invalid read of size 8
==2465==    at 0x407AC05: vfprintf (vfprintf.c:1622)
==2465==    by 0x4081D7F: printf (printf.c:35)
==2465==    by 0x8048528: main (test2.c:16)
==2465==  Address 0xbe4480e4 is on thread 1's stack

c) The errors are clearly related to reading and writing from the array. This simpler version of the program does not cause any Valgrind errors:

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

int main(){
  srand(time(NULL));
  int i;
  for(i=0;i<1000000;i++){
     if (rand()*(1.0)/(double)RAND_MAX == 1.0){
         printf("Wow!\n");
     }
  }
  return 0;
}
1''
  • 26,823
  • 32
  • 143
  • 200
  • Your completely correct. I do not know why I am even using an array for the random numbers, so I will scrap the array. Thanks a bunch! – Shinobii Nov 22 '12 at 17:57