我需要一个动态数组,所以我在我的代码中使用了malloc ...但是我不知道如何在之后成功释放内存。在我的代码中的某处,我相信我有一个指针重新分配,导致悬空指针错误(当我做child2 = child1时)。有谁知道如何正确释放我的mallocs?提前谢谢。
我的实际代码如下:
private void Button_Loaded(object sender, RoutedEventArgs e)
{
Button _button = (Button)sender;
if (string.IsNullOrEmpty(_button.Content.ToString()))
_button.IsEnabled = false;
}
在我的一个函数中,我有以下内容,其中pop_size和num_nodes之前分别计算为100和10。
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;
}
另外,在释放内存之前,我是否必须首先检查内存是否已正确分配?
答案 0 :(得分:1)
child1 = malloc(num_nodes * sizeof(child1));
这是不正确的。您正在为num_nodes指针分配空间(child1是指向Chrom的指针)。您想为num_nodes Chrom实例分配空间。 将其更改为
child1 = malloc(num_nodes * sizeof(*child1));
答案 1 :(得分:0)
首先,你为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。对于child2来说也是一样。
这可能接近你想要的:
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中使用edegs的2D数组,那么代码就是这样的;此外,我之前的回答中有一个错误; x应该在for循环中初始化为零而不是1,因为这将导致数组索引超出边界并使用低于 - 而不是低于或等于。 (警告:我只是略微测试过):
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;
}