***检测到glibc ***项目/调试/项目:free():

时间:2012-04-13 07:30:09

标签: c free memory-corruption

我正在努力解决这个错误很长一段时间。如果有问题,我会给出一些问题的快照。请指导我该怎么办??????它看起来像是一团糟。

*** glibc detected *** /home/shivam/workspace/Project/Debug/Project: free(): invalid next size (fast): 0x099781a0 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6ff22)[0x17ff22]
/lib/i386-linux-gnu/libc.so.6(+0x70bc2)[0x180bc2]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0x183cad]
/home/shivam/workspace/Project/Debug/Project[0x8048a57]
/home/shivam/workspace/Project/Debug/Project[0x8048fce]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x129113]
/home/shivam/workspace/Project/Debug/Project[0x8048541]
======= Memory map: ========
00110000-00288000 r-xp 00000000 08:08 786579     /lib/i386-linux-gnu/libc-2.13.so
00288000-0028a000 r--p 00178000 08:08 786579     /lib/i386-linux-gnu/libc-2.13.so
0028a000-0028b000 rw-p 0017a000 08:08 786579     /lib/i386-linux-gnu/libc-2.13.so
0028b000-0028e000 rw-p 00000000 00:00 0 
0039e000-003ba000 r-xp 00000000 08:08 787384     /lib/i386-linux-gnu/libgcc_s.so.1
003ba000-003bb000 r--p 0001b000 08:08 787384     /lib/i386-linux-gnu/libgcc_s.so.1
003bb000-003bc000 rw-p 0001c000 08:08 787384     /lib/i386-linux-gnu/libgcc_s.so.1
0040f000-00410000 r-xp 00000000 00:00 0          [vdso]
00b16000-00b34000 r-xp 00000000 08:08 786576     /lib/i386-linux-gnu/ld-2.13.so
00b34000-00b35000 r--p 0001d000 08:08 786576     /lib/i386-linux-gnu/ld-2.13.so
00b35000-00b36000 rw-p 0001e000 08:08 786576     /lib/i386-linux-gnu/ld-2.13.so
08048000-0804a000 r-xp 00000000 08:08 1604       /home/shivam/workspace/Project/Debug/Project
0804a000-0804b000 r--p 00001000 08:08 1604       /home/shivam/workspace/Project/Debug/Project
0804b000-0804c000 rw-p 00002000 08:08 1604       /home/shivam/workspace/Project/Debug/Project
09978000-09999000 rw-p 00000000 00:00 0          [heap]
b7700000-b7721000 rw-p 00000000 00:00 0 
b7721000-b7800000 ---p 00000000 00:00 0 
b7829000-b782a000 rw-p 00000000 00:00 0 
b783e000-b7840000 rw-p 00000000 00:00 0 
bf8c6000-bf8e7000 rw-p 00000000 00:00 0          [stack]

实际代码如下:

/*
 * de.c
 *
 *  Created on: 2012-04-11
 *      Author: shivam
 */

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "const.h"

int simulate_de(result * input, double * bestarr) {

    /* random seed */
    srand((unsigned) time(NULL));
    int i, j;

    /* initial population */
    double ** population = input->population;

    /* generating random population */
    double ** new_pop = (double **) malloc(POPULATION_SIZE * sizeof(double *));

    /* Initialise the dynamic array */
    for (i = 0; i < POPULATION_SIZE; i++)
        new_pop[i] = (double *) malloc(DIMENSION * sizeof(double));

    // for(i = 0; i < POPULATION_SIZE; i++) {
    // for(j = 0; j < DIMENSION; j++) {
    // population[i][j] = (input->min - input->max) * ((double)rand() / RAND_MAX) + input->max;
    // }
    // }

    /* simulation length */
    int sim_len = MAX_NFC * DIMENSION;

    /* for randomly choosing the three random numbers */
    int rand_chooser[POPULATION_SIZE];

    /* Initialise the random chooser array */
    for (i = 0; i < POPULATION_SIZE; i++)
        rand_chooser[i] = i;

    char * _boolMap;
    _boolMap = (char *)malloc((sim_len + 1) * POPULATION_SIZE * sizeof(char));
    memset(_boolMap, 0, (sim_len + 1) * POPULATION_SIZE * sizeof(char));

    double * _valMap;
    _valMap = (double *)malloc((sim_len + 1) * POPULATION_SIZE * sizeof(double));

    int nfc = 0, X;
    double solution, trial_solution, best_temp, best;

    /* trial vector */
    double U[DIMENSION];
    /* noisy vector */
    double V[DIMENSION];

    while (nfc < sim_len) {
        best = 0x7ff0000000000000;

        for (i = 0; i < POPULATION_SIZE; i++) {
            /* choosing randomly */
            int _rand[3];

            for (j = 0; j < 3; j++) {
                int idx;
                /* generate random number not equal to I */
                while ((idx = rand() % (POPULATION_SIZE - j)) == i);
                _rand[j] = rand_chooser[idx];

                /* swap */
                rand_chooser[idx] = rand_chooser[POPULATION_SIZE - 1];
                rand_chooser[POPULATION_SIZE - 1] = _rand[j];
            }

            /* add values to noisy vector */
            for (j = 0; j < DIMENSION; j++) {
                V[j] = population[_rand[0]][j]
                        + F
                                * (population[_rand[2]][j]
                                        - population[_rand[1]][j]);
                if (V[j] < input->min)
                    V[j] = input->min;
                else if (V[j] > input->max)
                    V[j] = input->max;
            }

            for (j = 0; j < DIMENSION; j++) {
                if (((double) rand() / RAND_MAX) < CR)
                    U[j] = V[j];
                else
                    U[j] = population[i][j];
            }

            X = (sim_len + 1) * nfc + i;
            if(_boolMap[X] == DEFINED)
                solution = _valMap[X];
            else {
                solution = input->function(population[i], DIMENSION);
                _valMap[X] = solution;
                _boolMap[X] = DEFINED;
            }
            trial_solution = input->function(U, DIMENSION);

            /* replace into new population */
            if (trial_solution <= solution) {
                for (j = 0; j < DIMENSION; j++)
                    new_pop[i][j] = U[j];
                best_temp = trial_solution;
            } else {
                for (j = 0; j < DIMENSION; j++)
                    new_pop[i][j] = population[i][j];
                best_temp = solution;
            }

            X += sim_len + 1;
            /* next population */
            _valMap[X] = best_temp;
            _boolMap[X] = DEFINED;

            if (best_temp < best)
                best = best_temp;
        }

        /* add best fitness */
        bestarr[nfc] = best;

        /* replace the population */
        for (i = 0; i < POPULATION_SIZE; i++) {
            for (j = 0; j < DIMENSION; j++) {
                population[i][j] = new_pop[i][j];
            }
        }

        /* increment the NFC */
        nfc += 1;
    }
    /* free up all the dynamic memory */
    for (i = 0; i < POPULATION_SIZE; i++) {
        free(new_pop[i]);
    }
    free(new_pop);
    free(_boolMap);
    free(_valMap);
    return 0;
}

我尝试了调试器,导致此错误的行是free(_boolMap),即最后一个第三行。

1 个答案:

答案 0 :(得分:1)

_valMap_boolMap的大小为(sim_len + 1) * POPULATION_SIZE。 确保您的X不超过该尺寸。

如果您可以发布main()const.h,可能会有帮助 问题有点容易......