2

次のように構造体の配列を返す関数があります。

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;
}

関数を呼び出して配列を使用した後、メモリを解放します。

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);

したがって、コメントされたコード行にSIGABRTがあります。

glibcが検出されました***./exec_main:ダブルフリーまたは破損(!prev):0x0000000013f74720 ***
=======バックトレース:========= /lib/libc.so.6[0x30004762f6] /lib/libc.so.6(cfree+0x6c)[0x300047ac6c]

私を助けてくれてありがとう。

4

1 に答える 1

0
my_struct * testFunction(int pSize, int pW_size)
{
   return (my_struct *)malloc((sizeof(my_struct) + sizeof(double) * pW_size) * pSize);
}
于 2012-04-28T11:34:00.060 に答える