⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memory.c

📁 遗传算法的简单程序
💻 C
字号:
//内存分配 

/*----------------------------------------------------------------------------*/
/* 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -