Basically, I am creating 2D array of struct using calloc(). Then I utilize that array and I free that allocated space, while freeing it I am getting "double free or corruption (!prev)". Code is written in C language.
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <fcntl.h>
#include <malloc.h>
#include <unistd.h>
#include <string.h>
#include <float.h>
typedef struct complex_num{
double real;
double imag;
}comp;
void transpose(comp *arr, int height, int width);
void change(comp *arr, int width);
void main(){
int width = 64, height = 64;
int len = width;
comp *fft[len];
for(int i=0; i<len; i++){
fft[i] = (comp *)calloc(len, sizeof(comp));
}
for (int scan=0;scan<height;scan++){
for (int pix=0;pix<width;pix++){
fft[scan][pix].real = 1.0;
fft[scan][pix].imag = 0.0;
}
change(&fft[scan][0], len);
}
transpose(&fft[0][0], len, len);
for(int i=0;i<len;i++){
change(&fft[i][0], len);
}
transpose(&fft[0][0], len, len);
for(int i=0;i<len;i++){
free(fft[i]);
}
}
void transpose(comp *arr, int height, int width){
comp var;
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
if(j>i){
var = *((arr + (i*width)) + j);
*((arr + i*width) + j) = *((arr + j*width) + i);
*((arr + j*width) + i) = var;
}
}
}
}
void change(comp *arr, int width){
for(int i=0; i<width; i++){
(arr + i)->real = 5.0;
(arr + i)->imag = 6.9;
}
}
Error message:
*** Error in `./a.out': double free or corruption (!prev): 0x0000000002095010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f05108a77e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f05108b037a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f05108b453c]
./a.out[0x400880]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f0510850830]
./a.out[0x400509]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:00 467935 /path/to/a.out
00600000-00601000 r--p 00000000 00:00 467935 /path/to/a.out
00601000-00602000 rw-p 00001000 00:00 467935 /path/to/a.out
02095000-020b6000 rw-p 00000000 00:00 0 [heap]
7f050c000000-7f050c021000 rw-p 00000000 00:00 0
7f050c021000-7f0510000000 ---p 00000000 00:00 0
7f0510610000-7f0510626000 r-xp 00000000 00:00 360788 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0510626000-7f0510825000 ---p 00000016 00:00 360788 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0510825000-7f0510826000 rw-p 00015000 00:00 360788 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0510830000-7f05109f0000 r-xp 00000000 00:00 360750 /lib/x86_64-linux-gnu/libc-2.23.so
7f05109f0000-7f05109f9000 ---p 001c0000 00:00 360750 /lib/x86_64-linux-gnu/libc-2.23.so
7f05109f9000-7f0510bf0000 ---p 000001c9 00:00 360750 /lib/x86_64-linux-gnu/libc-2.23.so
7f0510bf0000-7f0510bf4000 r--p 001c0000 00:00 360750 /lib/x86_64-linux-gnu/libc-2.23.so
7f0510bf4000-7f0510bf6000 rw-p 001c4000 00:00 360750 /lib/x86_64-linux-gnu/libc-2.23.so
7f0510bf6000-7f0510bfa000 rw-p 00000000 00:00 0
7f0510c00000-7f0510c25000 r-xp 00000000 00:00 360690 /lib/x86_64-linux-gnu/ld-2.23.so
7f0510c25000-7f0510c26000 r-xp 00025000 00:00 360690 /lib/x86_64-linux-gnu/ld-2.23.so
7f0510e25000-7f0510e26000 r--p 00025000 00:00 360690 /lib/x86_64-linux-gnu/ld-2.23.so
7f0510e26000-7f0510e27000 rw-p 00026000 00:00 360690 /lib/x86_64-linux-gnu/ld-2.23.so
7f0510e27000-7f0510e28000 rw-p 00000000 00:00 0
7f0510e30000-7f0510e31000 rw-p 00000000 00:00 0
7f0510e40000-7f0510e41000 rw-p 00000000 00:00 0
7f0510e50000-7f0510e51000 rw-p 00000000 00:00 0
7f0510e60000-7f0510e61000 rw-p 00000000 00:00 0
7fffc47c7000-7fffc4fc7000 rw-p 00000000 00:00 0 [stack]
7fffc5242000-7fffc5243000 r-xp 00000000 00:00 0 [vdso]
Aborted (core dumped)
I am compiling my code with GCC version 5.4.0. I don't understand the error message and don't know how to debug it. What should I do to free the array of pointers?