2012-04-13 5 views
-1

Ich kämpfe seit langem mit diesem Fehler. Ich gebe ein paar Schnappschüsse von dem Problem, wenn sie helfen. Bitte führe mich wie soll ich fortfahren ????? Es sieht aus wie ein Chaos für mich.*** glibc entdeckt *** Projekt/Debug/Projekt: free():

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

der eigentliche Code ist unten:

/* 
* 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 der Debugger versucht, und die Linie, die diesen Fehler verursachte, war free(_boolMap) d.h. die letzte Zeile thrid.

+2

helfen haben Sie versucht, einen Debugger mit der bestimmten Zeile zu finden, die diesen Fehler verursacht? – mfrankli

+1

Sieht aus wie Heap-Korruption. Vielleicht schreibst du mehr als nur Malloced. Verfolgen Sie Ihre malloced Zeiger –

+3

Führen Sie Ihr Programm mit valgrind & es sollte Ihnen sagen, was los ist. –

Antwort

1

_valMap und _boolMap sind von Größe (sim_len + 1) * POPULATION_SIZE. Stellen Sie sicher, dass Ihre X diese Größe nicht überschreitet.

Kann sein, wenn Sie main() und const.h veröffentlichen können, wäre es Antwort die Frage ein wenig einfacher ...

+0

Vielen Dank. Kurz bevor ich deinen Kommentar gelesen habe, habe ich das herausgefunden. Aber danke für den Fehler. – Shivam