释放内存时出现奇怪的C错误?

时间:2013-02-28 17:34:16

标签: c free pointer-to-pointer

我仍然试图让我的遗传算法工作(昨天我遇到内存分配问题,因此在尝试释放它时出现了一个可怕的错误)但今天我有这段代码

while (iCurrentGen <= data->m_iMaxGenerations)
{
    arrfSelectedChromosomes = selection(&data[0], szChromosomes);
    iSelectedLen = order_descending_grid(arrfSelectedChromosomes);
    szAuxGen = crossover(&data[0], arrfSelectedChromosomes, szChromosomes, iSelectedLen);
    //szChromosomes is what I need to free after that call returns
    free_generation(&data[0], szChromosomes);//Code Explotion
    szChromosomes = szAuxGen;
    szAuxGen = NULL;
    ++iCurrentGen;
}

我在free_generation()函数调用之前检查了它的内容,它类似于下图:

(变量前四个值)

Variable first four values

但是在free_generation()函数中,同一个变量丢失了一些值(特别是当i取值为2时,i为for循环索引),如下所示: / p>

(函数内的变量值)

Variable values inside the function

我在下面发布了free_generation代码:

void free_generation(struct INPUT_DATA* d, char** szChromosomes)
{
    int i;

    for (i = 0; i < d->m_iPopulationSize; ++i)
    {
        free(szChromosomes[i]);
    }

    free(szChromosomes);
    szChromosomes = NULL;
}

szChromosomes的定义如下:

char** szChromosomes = (char**)malloc(d->m_iPopulationSize * sizeof(char*));

srand(time(NULL));
for (i = 0; i < d->m_iPopulationSize; ++i)
{
    szChromosomes[i] = (char*)malloc((d->m_iBitsPChromosome + 1) * sizeof(char));
    for (j = 0; j < d->m_iBitsPChromosome; ++j)
    {
        szChromosomes[i][j] = rand_1_0(0.0, 1.0) == 1? '1' : '0';
    }
    szChromosomes[i][j] = '\0';
}

我需要澄清,这个值的丢失只发生在最后一次发布的while循环的第二次迭代之后。我的意思是在第一次运行时一切都很完美但是在这次迭代之后,第二次运行如上所述。

编辑:

我忘了包含循环控制变量的增量(感谢指出它!不,它不是全局xD)。我包括了交叉代码的一部分:

    char** crossover(struct INPUT_DATA* d, float** arrfSelectedChromosomes, char** arrszChromosomes, int iChromosomesInGrid)
{
    int i;
    int iTIndex = 0, iRPos = 0;
    char* szFirstChromosome = NULL;
    char* szSecondChromosome = NULL;
    char* szFirstNewChrom = (char*)malloc((d->m_iBitsPChromosome + 1) * sizeof(char));
    char* szSecondNewChrom = (char*)malloc((d->m_iBitsPChromosome + 1) * sizeof(char));

    char** arrszNewPop = (char**)malloc(d->m_iPopulationSize * sizeof(char*));

    int iSplitPoint = (int)(d->m_iBitsPChromosome / 4);

    float fCrossOverProb = CROSSOVER_PROBABILITY; 

    srand(time(NULL));
    for (i = 0; i < d->m_iPopulationSize; i += 2)
    {
        iRPos = rand() % iChromosomesInGrid;
        iTIndex = (int)arrfSelectedChromosomes[iRPos][0];
        szFirstChromosome = arrszChromosomes[iTIndex];
        iRPos =  rand() % iChromosomesInGrid;
        iTIndex = (int)arrfSelectedChromosomes[iRPos][0];
        szSecondChromosome = arrszChromosomes[iTIndex];

        if (is_same_chromosome(szFirstChromosome, szSecondChromosome))
        {
            i -= 2;
            continue;
        }

        if (fCrossOverProb < CROSSOVER_PROBABILITY)
        {
            //if probability is lower than the defined prob. we keep both chromosomes
            strcpy(szFirstNewChrom, szFirstChromosome);
            strcpy(szSecondNewChrom, szSecondChromosome);
        }
        else
        {

            strcpy(szFirstNewChrom, szFirstChromosome);
            szFirstNewChrom[iSplitPoint] = '\0';
            strcat(szFirstNewChrom, &szSecondChromosome[iSplitPoint]);

            //Para crear el segundo hijo se realiza una operacion similar
            strcpy(szSecondNewChrom, szSecondChromosome);
            szSecondNewChrom[iSplitPoint] = '\0';
            strcat(szSecondNewChrom, &szFirstChromosome[iSplitPoint]);
        }

        arrszNewPop[i] = szFirstNewChrom;
        arrszNewPop[i + 1] = szSecondNewChrom;
    }

    return arrszNewPop;
}

1 个答案:

答案 0 :(得分:1)

由于对mark a comment as an answer的功能请求仍然被拒绝,我在此处复制上述解决方案。

你是那个男人!非常感谢..这是一个重复指针的问题,这是由于交叉功能中错误的内存分配造成的。我在函数开头为szFirstNewChrom和szSecondNewChrom分配内存,但该内存用于30个不同的字符串。由于这个可怕的错误,free_generation函数一直在失败,因为它试图释放先前释放的指针。谢谢你们! - Jorge Cespedes