我需要一个动态数组,所以我在代码中使用了malloc。。。然而,我不知道如何在之后成功地释放内存。在我的代码中,我相信我有一个指针重新分配,这会导致悬挂指针错误(当我执行child2=child1时)。有人知道如何正确地释放我的锦葵吗?提前谢谢。
我的实际代码如下:
typedef struct Edge//per solution
{
int label;//label
float weight;//energy of each edge
} edge;
// creating the chrom structure
typedef struct Chrom
{
edge **gene;
float fitness_score;
}
在我的一个函数中,我有以下内容,其中pop_size和num_nodes之前分别计算为100和10。
Chrom* child1;
Chrom* child2;
//allocate memory of child
child1 = malloc(num_nodes * sizeof(child1));
child2 = malloc(num_nodes * sizeof(child2));
if(child1 == NULL||child2 == NULL)
printf("ERROR1: Memory allocation failed!");
for(x = 1; x <= num_nodes; x++)
{
child1[x].gene = malloc(num_nodes * sizeof(edge*));
child2[x].gene = malloc(num_nodes * sizeof(edge*));
if(child1[x].gene == NULL||child2[x].gene == NULL)
printf("ERROR2: Memory allocation failed!");
for(y = 0; y < num_nodes; y++)
{
child1[x].gene[y] = malloc(num_nodes * sizeof(edge));
child2[x].gene[y] = malloc(num_nodes * sizeof(edge));
if(child1[x].gene[y] == NULL||child2[x].gene[y] == NULL)
printf("ERROR3: Memory allocation failed!");
}
}
//do something...
for(i=0; i<pop_size; i++)
for(x=0; x<num_nodes; x++)
for(y=0;y<num_nodes;y++)
child2[i].gene[x][y].label=child1[i].gene[x][y].label;
free(child1);//can i free the memory like this?
free (child2);// will it automatically do all 'arrays'?
另外,在释放内存之前,我必须先检查内存是否分配正确吗?
child1 = malloc(num_nodes * sizeof(child1));
这是不正确的。您正在为num_nodes指针分配空间(child1是指向Chrom的指针)。您想要为num_nodes Chrom实例分配空间。将其更改为
child1 = malloc(num_nodes * sizeof(*child1));
首先,你为Chrom指针分配空间,而不是为Chrom结构分配空间,所以我很惊讶child1[x].gene在不崩溃的情况下工作,但只回答代码中作为注释提出的问题,
free(child1);//can i free the memory like this?
free (child2);// will it automatically do all 'arrays'?
child1是一个指针数组,每个指针指向分配的内存,这些内存在释放时将丢失(child1)。我会先释放每个指针child1[x].gene,然后再释放child1。孩子2也是一样。
这可能接近你想要的:
typedef struct Edge//per solution
{
int label;//label
float weight;//energy of each edge
} edge;
// creating the chrom structure
typedef struct Chrom
{
edge *gene; // changed from edge**
float fitness_score;
};
int main(void)
{
int num_nodes = 3;
int x;
struct Chrom* child1;
// if you want num_nodes Chrom entries
child1 = malloc(num_nodes * sizeof(struct Chrom));
// Allocating individual edges (I don't know why you declare edge** gene
// so I will assume that what you intended was edge* gene
for(x = 1; x <= num_nodes; x++)
{
child1[x].gene = (edge*)malloc(sizeof(struct Edge));
}
// deallocate your memory
for(x = 1; x <= num_nodes; x++)
{
free(child1[x].gene);
}
// free your array of Chroms
free(child1);
return 0;
}
如果你想在每个Chrom中有一个2D阵列的edeg,下面是代码;此外,我之前的回答中有一个错误;在for循环中,x应该初始化为零,而不是1,因为这将导致数组索引越界,并使用lower than而不是lower than或equal。(警告:我只是稍微测试了一下):
typedef struct Edge//per solution
{
int label;//label
float weight;//energy of each edge
} edge;
// creating the chrom structure
typedef struct Chrom
{
edge **gene;
float fitness_score;
};
int main(void)
{
int num_nodes = 3;
int num_edges_x = 2;
int num_edges_y = 3;
int x, j;
struct Chrom* child1;
// if you want num_nodes Chrom entries
child1 = malloc(num_nodes * sizeof(struct Chrom));
// Allocating 2D array of edges for each Chrom
// USE zero-based indexing.
for(x=0; x < num_nodes; x++)
{
child1[x].gene = (edge**)malloc(num_edges_x * sizeof(edge*));
// initialise you array of edges
for (j=0; j<num_edges_x; j++)
{
child1[x].gene[j] = (edge*)malloc(num_edges_y * sizeof(edge));
}
}
// Use a child1[x].gene[x][y]
child1[0].gene[0][0].label = 3;
child1[0].gene[0][0].weight = 7.2F;
printf("nlabel: %d - weight: %f", child1[0].gene[0][0].label, child1[0].gene[0][0].weight);
child1[1].gene[0][0].label = 1;
child1[1].gene[0][0].weight = 12.4F;
printf("nlabel: %d - weight: %f", child1[1].gene[0][0].label, child1[1].gene[0][0].weight);
child1[1].gene[1][0].label = 5;
child1[1].gene[1][0].weight = 112.6F;
printf("nlabel: %d - weight: %f", child1[1].gene[1][0].label, child1[1].gene[1][0].weight);
// deallocate your memory
for(x =0; x < num_nodes; x++)
{
for (j=0; j<num_edges_x; j++)
{
free(child1[x].gene[j]);
}
free(child1[x].gene);
}
free(child1);
return 0;
}