I have a function which returns an array of struct as follows:
my_struct * testFunction(int pSize, int pW_size) {
struct my_struct* struct_array = (my_struct*) malloc(sizeof(my_struct)*pSize);
for(int i=0; i<pSize; i++) {
struct my_struct test;
test.w_i = (double*) malloc(sizeof(double)*pW_size);
struct_array[i] = test;
}
return struct_array;
}
After calling the function and using the array I free the memory:
struct my_struct * T;
T=testFunction(theSize,wSize);
.....
for (int i = 0; i < theSize; i++)
free(T[i].w_i); // I have a SIGABRT in this line
free(T);
So I have SIGABRT in the commented line of code.
glibc detected *** ./exec_main: double free or corruption (!prev): 0x0000000013f74720 *** ======= Backtrace: ========= /lib/libc.so.6[0x30004762f6] /lib/libc.so.6(cfree+0x6c)[0x300047ac6c]
Thanks for helping me.