2012-04-13 87 views
-1

我現在正在努力解決這個錯誤很長時間。如果他們幫助,我會給出一些問題的快照。請指導我如何着手?它看起來像一個混亂給我。*** glibc detected ***項目/調試/項目:免費():

*** 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)即最後THRID線。

+2

你嘗試過使用調試器來查找導致此錯誤的特定行? – mfrankli 2012-04-13 07:33:42

+1

看起來像堆腐敗。你可能會寫更多的混合區域。跟蹤你的malloced指針 – 2012-04-13 07:37:31

+3

用valgrind運行你的程序,它應該告訴你什麼是錯的。 – 2012-04-13 07:40:32

回答

1

_valMap_boolMap的尺寸爲(sim_len + 1) * POPULATION_SIZE。 確保您的X不超出這個尺寸。

可能是,如果你可以張貼main()const.h,這將有助於回答 問題更容易一點......

+0

非常感謝。在閱讀你的評論之前,我想清楚了。但是,謝謝你指出這個錯誤。 – Shivam 2012-04-13 07:56:20