質問

I'm struggling with this error for quite long now. I am giving some snapshots of the problem if they help. Please guide me how should I proceed????? Its looking like a mess to me.

*** 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]

The actual code is below:

/*
 * 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;
}

I tried the debugger and the line that caused this error was free(_boolMap) i.e. the last thrid line.

役に立ちましたか?

解決

_valMap and _boolMap are of size (sim_len + 1) * POPULATION_SIZE. Make sure that your X doesn't go beyond that size.

May be if you can post main() and const.h, it would help answer the question a bit easier...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top