gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:23:44 36 4
gpt4 key购买 nike

我已经为这个错误苦苦挣扎了很长时间。如果有帮助的话,我会提供一些问题的快照。请指导我该怎么办????在我看来,它看起来一团糟。

*** 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) 即最后第三行。

最佳答案

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

也许如果您可以发布 main()const.h,这将有助于回答这个问题稍微简单一点...

关于c - *** 检测到 glibc *** 项目/调试/项目 : free():,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10136874/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com