Ошибка «double free or corruption» в динамическом двумерном массиве
*** Error in `./a.out’: double free or corruption (out): 0x000000000060f030 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fbb1e1fb7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7fbb1e20437a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fbb1e20853c]
./a.out[0x400895]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fbb1e1a4830]
./a.out[0x400539]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:02 46140927 /home/dk/Документы/Programms/a.out
00600000-00601000 r—p 00000000 08:02 46140927 /home/dk/Документы/Programms/a.out
00601000-00602000 rw-p 00001000 08:02 46140927 /home/dk/Документы/Programms/a.out
0060f000-00630000 rw-p 00000000 00:00 0 [heap]
7fbb18000000-7fbb18021000 rw-p 00000000 00:00 0
7fbb18021000-7fbb1c000000 —p 00000000 00:00 0
7fbb1df6e000-7fbb1df84000 r-xp 00000000 08:02 21762449 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fbb1df84000-7fbb1e183000 —p 00016000 08:02 21762449 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fbb1e183000-7fbb1e184000 rw-p 00015000 08:02 21762449 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fbb1e184000-7fbb1e344000 r-xp 00000000 08:02 21758151 /lib/x86_64-linux-gnu/libc-2.23.so
7fbb1e344000-7fbb1e544000 —p 001c0000 08:02 21758151 /lib/x86_64-linux-gnu/libc-2.23.so
7fbb1e544000-7fbb1e548000 r—p 001c0000 08:02 21758151 /lib/x86_64-linux-gnu/libc-2.23.so
7fbb1e548000-7fbb1e54a000 rw-p 001c4000 08:02 21758151 /lib/x86_64-linux-gnu/libc-2.23.so
7fbb1e54a000-7fbb1e54e000 rw-p 00000000 00:00 0
7fbb1e54e000-7fbb1e574000 r-xp 00000000 08:02 21758137 /lib/x86_64-linux-gnu/ld-2.23.so
7fbb1e749000-7fbb1e74c000 rw-p 00000000 00:00 0
7fbb1e772000-7fbb1e773000 rw-p 00000000 00:00 0
7fbb1e773000-7fbb1e774000 r—p 00025000 08:02 21758137 /lib/x86_64-linux-gnu/ld-2.23.so
7fbb1e774000-7fbb1e775000 rw-p 00026000 08:02 21758137 /lib/x86_64-linux-gnu/ld-2.23.so
7fbb1e775000-7fbb1e776000 rw-p 00000000 00:00 0
7ffe26cec000-7ffe26d0d000 rw-p 00000000 00:00 0 [stack]
7ffe26d11000-7ffe26d14000 r—p 00000000 00:00 0 [vvar]
7ffe26d14000-7ffe26d16000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Аварийный останов (сделан дамп памяти)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#define _CRT_SECURE_NO_WARNINGS #include #include #include int main(int argc, char*argv[]){ int a = 5; int b = 16; int i = 0, j = 0; int ** M = (int **)malloc(sizeof(int) * a); for (i = 0; i a; i++){ M[i]=(int *)malloc(sizeof(int) * b); for (j = 0; j b; j++){ M[i][j] = j; printf("%d\t", M[i][j]); } printf("\n"); } printf("\n"); for (j = 4; j 7; j++){ for (i = 0; i 5; i++){ int S = M[i][j]; M[i][j] = M[i][14-j]; M[i][14-j] = S; } } for (i = 0; i a; i++){ for (j = 0; j b; j++){ printf("%d\t", M[i][j]); } printf("\n"); } for (i=0; i a; i++){ free(M[i]); } free(M); return 0; }
double free or corruption (out)
Есть класс Matrix , в конструкторе память динамически резервируется, в деструкторе — освобождается, но при попытке освободить память деструктор терпит фиаско с сообщением double free or corruption (out) . Это происходит если cols>rows , т.е при Matrix(3, 3) исключения нет, а вот при Matrix(3, 4) есть.
struct Matrix < double **a; unsigned rows, cols; Matrix(const unsigned rows, const unsigned cols): rows(rows), cols(cols) < a = new double*[rows]; for (unsigned i = 0; i < cols; ++i) < a[i] = new double[cols]; >> ~Matrix() < for (unsigned i = 0; i < rows; ++i) < delete []a[i]; // double free or corruption (out) >delete []a; > >; int main()
Пробовал использовать malloc/calloc/free — та же ошибка.

neversleep ★★
21.01.18 10:26:17 MSK
Ошибка double free or corruption
Есть следующий функции: Функция, сохраняющая текст неизвестного размера в память (Переменная sentence отвечает за количество предложений):
char** text_input(char **text, int *sentence) < int slen = 50; char ch; int symb = 0; while (ch != '\n')< text[*sentence] = (char*)malloc(slen * sizeof(char)); while (ch != '.' && ch != '\n')< ch = getchar(); if (ch == '\n')< break; >if (symb >= slen - 1) < slen += 50; text[*sentence] = (char*)realloc(text[*sentence], slen * sizeof(char)); >text[*sentence][symb++] = ch; > if (ch == '\n') < break; >ch = '\0'; text[*sentence][symb] = ch; *sentence += 1; text = (char**)realloc(text, (*sentence + 1) * sizeof(char*)); symb = 0; slen = 50; > return text; >
И функция, удаляющая из текста одинаковые предложения:
char** text_redaction(char **text, int *sentence) < int count = 0; for(int sentence1 = 0; sentence1 < *sentence - 1; sentence1++)< for (int sentence2 = sentence1 + 1; sentence2 < *sentence; sentence2++)< if (strlen(text[sentence1]) == strlen(text[sentence2]))< for(int j = 0; j < strlen(text[sentence1]); j++)< if (toupper(text[sentence1][j]) == toupper(text[sentence2][j]))< ++count; >> if (count == strlen(text[sentence1])) < free(text[sentence2]); for (int l = sentence2; l < *sentence; l++)< text[l] = text[l+1]; >text[*sentence - 1] = NULL; sentence2 -= 1; *sentence -= 1; count = 0; > > > > return text; >
При запуске вторая выдает ошибку: double free or corruption Где ошибка?
Отслеживать
задан 9 дек 2018 в 21:17
23 4 4 бронзовых знака
«При запуске» чего? Как выглядит вызывающий код?
9 дек 2018 в 21:53
Реалок странный)
9 дек 2018 в 22:09
В каком месте «double free or corruption»? В какой строке? На каком входе? Почему эта информация отсутствует в тексте вопроса?