allocate.c

来源:互联网 发布:burpsuite安装linux 编辑:程序博客网 时间:2024/06/17 05:10
/* Memory allocation and deallocation routines */# include <stdio.h># include <stdlib.h># include <math.h># include "global.h"# include "rand.h"/* Function to allocate memory to a population */void allocate_memory_pop (population *pop, int size){    int i;    pop->ind = (individual *)malloc(size*sizeof(individual));    for (i=0; i<size; i++)    {        allocate_memory_ind (&(pop->ind[i]));    }    return;}/* Function to allocate memory to an individual */void allocate_memory_ind (individual *ind){    int j;    if (nreal != 0)//nreal is a global variable,is the number of "real variables"    {        ind->xreal = (double *)malloc(nreal*sizeof(double));    }    if (nbin != 0)//nbin is a global variable, is the number of "binary variables"    {        ind->xbin = (double *)malloc(nbin*sizeof(double));        ind->gene = (int **)malloc(nbin*sizeof(int *));        for (j=0; j<nbin; j++)        {            ind->gene[j] = (int *)malloc(nbits[j]*sizeof(int));//nbits is an arrary of "nbin*(double)",and nbits[j] is initialized in main func(nsga2r.c)        }    }    ind->obj = (double *)malloc(nobj*sizeof(double));//nobj is a global variable, is the number of objectives    if (ncon != 0)    {        ind->constr = (double *)malloc(ncon*sizeof(double));//ncon is a global variable, is the number of constraints    }    return;}/* Function to deallocate memory to a population */void deallocate_memory_pop (population *pop, int size){    int i;    for (i=0; i<size; i++)    {        deallocate_memory_ind (&(pop->ind[i]));    }    free (pop->ind);    return;}/* Function to deallocate memory to an individual */void deallocate_memory_ind (individual *ind){    int j;    if (nreal != 0)    {        free(ind->xreal);    }    if (nbin != 0)    {        for (j=0; j<nbin; j++)        {            free(ind->gene[j]);        }        free(ind->xbin);        free(ind->gene);    }    free(ind->obj);    if (ncon != 0)    {        free(ind->constr);    }    return;}

源代码来源:http://www.iitk.ac.in/kangal/codes.shtml

中间添加了一些自己的注释,用下划线标识的。

(其实就是一些可以用SI查找一下啊就能找到这些变量的含义。只是希望记下来印象深刻一些吧。)


0 0
原创粉丝点击