memory.c

来自「遗传算法的简单程序」· C语言 代码 · 共 63 行

C
63
字号
//内存分配 

/*----------------------------------------------------------------------------*/
/* memory.c - memory management routines for sga code  */ 
/*----------------------------------------------------------------------------*/ 
#include "external.h"
 
initmalloc()         /* memory allocation of space for global data structures */ 
{ 
  unsigned nbytes; 
  char     *malloc(); 
  int j; 
  /* memory for old and new populations of individuals */ 
  nbytes = popsize*sizeof(struct individual); 
  if((oldpop = (struct individual *) malloc(nbytes)) == NULL) 
    nomemory(stderr,"oldpop"); 
  if((newpop = (struct individual *) malloc(nbytes)) == NULL) 
    nomemory(stderr,"newpop"); 
  /* memory for chromosome strings in populations */ 
  nbytes = chromsize*sizeof(unsigned); 
  for(j = 0; j < popsize; j++) 
  { 
      if((oldpop[j].chrom = (unsigned *) malloc(nbytes)) == NULL) 
        nomemory(stderr,"oldpop chromosomes"); 
      if((newpop[j].chrom = (unsigned *) malloc(nbytes)) == NULL) 
        nomemory(stderr,"newpop chromosomes"); 
  } 
  if((bestfit.chrom = (unsigned *) malloc(nbytes)) == NULL) 
    nomemory(stderr,"bestfit chromosome");
  
  /* allocate any auxiliary memory for selection */ 
  select_memory();
  
  /* call to application-specific malloc() routines   */ 
  /* can be used to malloc memory for utility pointer */ 
  app_malloc(); 
}

/* A routine to free all the space dynamically allocated in initspace() */ 
freeall() 
{ 
  int i; 
  for(i = 0; i < popsize; i++) 
  { 
      free(oldpop[i].chrom); 
      free(newpop[i].chrom); 
   } 
  free(oldpop); 
  free(newpop); 
  free(bestfit.chrom); 
  /* free any auxiliary memory needed for selection */ 
  select_free(); 
  /* call to application-specific free() routines   */ 
  /* can be used to free memory for utility pointer */ 
  app_free(); 
}
 
nomemory(string) 
     char *string; 
{ 
  fprintf(outfp,"malloc: out of memory making %s!!\n",string); 
  exit(-1); 
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?