Thread: Cygwin memory allocation / stackdumpfile

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    6

    Cygwin memory allocation / stackdumpfile

    I have my model in C which runs an evolutionary process, but once I input too large a population or too long an evolutionary period, it fails. For example, I can evolve a (constant) population of 20 individuals over 50 years in 14 seconds, but 30 individuals won't run. The error message is cygwin_exceptionpen_stackdumpfile: Dumping stack ... , which I believe is to do with memory allocation, but I tried using pflags to assign up to 4GB and it still doesn't run.

    The code is fairly long, but a simplified version is below (i know the functions all run as intended)

    Code:
    int main(){
    
    
    //structs initialised, controls like population size loaded
    //output file opened
    
    
    // initialise with artificial 'chromosome' using random number generation for each of N 'genes'
    Initialise(N, ctrl.Population, &chrom);
    
    
    
                for (int Generation = 1; Generation <= ctrl.Generations; Generation++){
    
    
                        for (int Individual = 1; Individual <= ctrl.Population; Individual++){
    
    
                            //calls function to assess fitness
    
    
                        } //END OF INDIVIDUAL LOOP
    
    
                        if(Generation < ctrl.Generations){
                                Selection(Generation, ctrl.Population, ctrl.MutationRate, N, &chrom);
                        }
    
    
                } // END OF GENERATION LOOP
     
        printf("Finished sim!");
    
    
    } /// END OF MAIN FUNCTION
    
    
    void Initialise(int N, int Population, chrom_struct *chrom)
    {
        double rand_double(); /* 0.0 to 1.0, unif. dist. */
    
    
        chrom->Genes  = (double **)malloc(Population * sizeof(double *));
        for(int i = 0; i < N; i++){
            chrom->Genes[i] = (double *)malloc(N * sizeof(double));
        }
        chrom->Fitness = (double *)malloc(Population * sizeof(double));
    
    
        struct timeval tm;
        gettimeofday(&tm, NULL);
        srandom(tm.tv_sec + tm.tv_usec * 1000000ul);
    
    
        for(int Individual = 1; Individual <= Population; Individual++){
            chrom->Genes[Individual][0] = 0;
            for(int i = 1; i<= N; i++){
                chrom->Genes[Individual][i] = rand_double();
            }
        }
    
    
    } // END OF INITIALISE
    
    
    double rand_double(){
        struct timeval tm;
        gettimeofday(&tm, NULL);
        srandom(tm.tv_sec + tm.tv_usec * 1000000ul);
        return random()/(double)RAND_MAX;
    }
    
    
    void Selection(int Generation, int Population, double MutationRate, int N, chrom_struct *chrom)
    {
        // copies current generation into parental array
        double **Parents;
        Parents = (double **)malloc(Population * sizeof(double *));
        for(int i = 0; i < soil->N; i++){
            Parents[i] = (double *)malloc(N * sizeof(double));
        }
    
    
        for(int Individual = 1; Individual <= Population; Individual++){
            for(int i = 1; i<= N; i++){
                Parents[Individual][i] = chrom->Genes[Individual][i];
            }
        }
    
    
        // ranks individuals  in current genes array depending on fitness
    
    
        // copies fittest individual over to parental array directly
    
    
        // for rest of pop, fills parental array from top half of individuals
    
        // overwrites genes array using randomly selected parents, recombines, mutates
    
    
    } // END OF SELECTION FUNCTION
    I'm trying to write a makefile etc to run it on the university's supercomputing cluster, but it's slow progress since I've never done it before. I was wondering if there's a way to get it to run on my laptop? Even 100 individuals x 100 generations would be fine.

    Thanks for your help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,308
    Does your Selection function free what it mallocs? I notice that you call the Selection function in a loop, so you might have a memory leak if Selection does not free what it mallocs, i.e., you keep accumulating memory allocated that you no longer need and cannot deallocate because you've "lost" the pointers to them.

    Technically you should also free what Initialise mallocs at some point, but those seem to be variables whose lifetime is that of the main function, so it is not so important because the OS would reclaim the memory allocated for them in due course.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,097
    i know all the functions run as intended
    No you don't. You think they all run as intended.

    For example, your rand_double function seems to be incorrect.
    You should only seed the rng once in the entire run of the program.
    So you should move the seeding to main.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    > for(int Individual = 1; Individual <= Population; Individual++)
    Yeah, your allocated arrays should be subscripted from 0 to N-1, not 1 to N.

    You're running off the end of every one of your malloc'ed arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    Quote Originally Posted by john.c View Post
    No you don't. You think they all run as intended.

    For example, your rand_double function seems to be incorrect.
    You should only seed the rng once in the entire run of the program.
    So you should move the seeding to main.
    Thanks - I've moved that out, tidies it up a bit.

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    Hi, thanks for replying. I overwrite those arrays continuously - it should work so that the current chromosome array is copied to the parents, then filled up, and after the fitness function is called, it's copied to parents again - so I don't think that's the issue. However, should I be freeing them at the end of the program? I don't know how long 'due course' means, don't want my computer storing a load of unnecessary info!

  7. #7
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    Thank you!!! Oh my goodness this worked! I'm used to working in VBA, so if I declare an array of size 10, it automatically creates an array of size 11, from 0-10. Technically I guess this makes more sense...

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,005
    Quote Originally Posted by Elfie View Post
    Thank you!!! Oh my goodness this worked! I'm used to working in VBA, so if I declare an array of size 10, it automatically creates an array of size 11, from 0-10. Technically I guess this makes more sense...
    No it goes from 0 to 9 !!!!!!!!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Cygwin and Windows access the same memory?
    By evb@studioe.com in forum C Programming
    Replies: 1
    Last Post: 02-15-2012, 11:18 PM
  2. please help on memory allocation
    By acfror in forum C Programming
    Replies: 4
    Last Post: 09-29-2005, 12:50 AM
  3. Memory allocation
    By Leiken in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2002, 04:32 PM
  4. Memory Allocation
    By Yankee in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2001, 03:16 PM
  5. Memory Allocation
    By Marcelo in forum C Programming
    Replies: 1
    Last Post: 10-26-2001, 08:43 AM

Tags for this Thread