释放matix [英] freeing a matix

查看:82
本文介绍了释放matix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何释放使用此功能创建的矩阵:


double ** my_matrix(long number_of_rows, long number_of_columns){

double **矩阵;

矩阵= calloc(number_of_rows,sizeof(double *));

int i; < ($ = 0; i< number_of_rows; i ++){

matrix [i] = calloc(number_of_columns,sizeof(double));

}

返回矩阵;

}

我的猜测是这样的,


void my_free_matrix (double ** m,long number_of_rows,long

number_of_columns){

int row;

for(row = 0; row< number_of_rows ;行++){

免费(m [row]);

}

免费(m);

}


但它失败并且*** glibc检测到***双重免费或损坏

(out):0xbfbbaa58 ***


非常感谢,

Michael Goerz

Hi,

how can I free a matrix that was created with this function:

double **my_matrix(long number_of_rows, long number_of_columns){
double **matrix;
matrix = calloc(number_of_rows, sizeof(double *));
int i;
for (i = 0; i < number_of_rows; i++){
matrix[i] = calloc(number_of_columns, sizeof(double));
}
return matrix;
}
My guess was this,

void my_free_matrix(double **m, long number_of_rows, long
number_of_columns){
int row;
for (row = 0; row < number_of_rows; row++){
free(m[row]);
}
free(m);
}

but it fails with "*** glibc detected *** double free or corruption
(out): 0xbfbbaa58 ***"

Many Thanks,
Michael Goerz

推荐答案

Michael Goerz写道:
Michael Goerz wrote:




如何释放使用此功能创建的矩阵:


double ** my_matrix(long number_of_rows,long number_of_columns){

double ** matrix;

matrix = calloc(number_of_rows,sizeof(double *));

int i;

for(i = 0;我< NUMBER_OF_ROWS; i ++){

matrix [i] = calloc(number_of_columns,sizeof(double));

}

返回矩阵;

}


我的猜测是这样的,


void my_free_matrix(double ** m,long number_of_rows,long

number_of_columns){

int row;

for(row = 0; row< number_of_rows; row ++){

free( m [row]);

}

免费(m);

}


但是它失败了*** glibc检测到***双免费或腐败

(out):0xbfbbaa58 ***"


非常感谢,

Michael Goerz
Hi,

how can I free a matrix that was created with this function:

double **my_matrix(long number_of_rows, long number_of_columns){
double **matrix;
matrix = calloc(number_of_rows, sizeof(double *));
int i;
for (i = 0; i < number_of_rows; i++){
matrix[i] = calloc(number_of_columns, sizeof(double));
}
return matrix;
}
My guess was this,

void my_free_matrix(double **m, long number_of_rows, long
number_of_columns){
int row;
for (row = 0; row < number_of_rows; row++){
free(m[row]);
}
free(m);
}

but it fails with "*** glibc detected *** double free or corruption
(out): 0xbfbbaa58 ***"

Many Thanks,
Michael Goerz



看起来对我来说正确。您是否尝试在调试器下运行以观察它?

可能在分配和

版本之间的行数不一致?

-

Bill Medland

Looks right to me. Have you tried running under a debugger to watch it?
Possibly the number of rows disagrees between the allocation and the
release?
--
Bill Medland


Bill Medland写道:
Bill Medland wrote:

> void my_free_matrix(double ** m,long number_of_rows,long
number_of_columns){
int row;
for(row = 0; row< number_of_rows; row ++){
free(m [row]);
}
free(m);
}

但它失败并且*** glibc检测到* **双倍免费或腐败
(出):0xbfbbaa58 ***"
>void my_free_matrix(double **m, long number_of_rows, long
number_of_columns){
int row;
for (row = 0; row < number_of_rows; row++){
free(m[row]);
}
free(m);
}

but it fails with "*** glibc detected *** double free or corruption
(out): 0xbfbbaa58 ***"



看起来对我来说正确。您是否尝试在调试器下运行以观察它?

可能在分配和

版本之间的行数不一致?

Looks right to me. Have you tried running under a debugger to watch it?
Possibly the number of rows disagrees between the allocation and the
release?



是的,我认为问题是我在一个

矩阵行中放置了一个静态数组。我想我不能那样做,可以吗?


int main(){

double ** m = my_matrix(4,3) ; //创建一个4x3矩阵

m [0] [1] = 5.0;

double row2 [] = {1.0,2.0,3.0,4.0};

m [1] = row2; //把它留下来,它不会崩溃

my_free_matrix(m,4,3);

返回0;

}

Yeah, I think the problem was that I put a static array in one of the
matrix rows. I guess I just can''t do that, can I?

int main(){
double **m = my_matrix(4,3); // create a 4x3 matrix
m[0][1] = 5.0;
double row2[] = {1.0, 2.0, 3.0, 4.0};
m[1] = row2; // leave this out, and it doesn''t crash
my_free_matrix(m, 4, 3);
return 0;
}


Michael Goerz写道:
Michael Goerz wrote:

Bill Medland写道:
Bill Medland wrote:

>> void my_free_matrix(double ** m,long number_of_rows,long
number_of_columns){
int row;
for(row = 0;行< number_of_rows;行++){
免费(m [row]);
}
免费(m);
}

但它失败了*** glibc检测到***双重免费或腐败
(out):0xbfbbaa58 ***"
>>void my_free_matrix(double **m, long number_of_rows, long
number_of_columns){
int row;
for (row = 0; row < number_of_rows; row++){
free(m[row]);
}
free(m);
}

but it fails with "*** glibc detected *** double free or corruption
(out): 0xbfbbaa58 ***"


看起来对我来说。您是否尝试在调试器下运行以观察它?
分配和
版本之间的行数可能不一致?

Looks right to me. Have you tried running under a debugger to watch it?
Possibly the number of rows disagrees between the allocation and the
release?



是的,我认为问题是我在一个

矩阵行中放置了一个静态数组。我想我不能那样做,可以吗?


Yeah, I think the problem was that I put a static array in one of the
matrix rows. I guess I just can''t do that, can I?



嗯,不,你不能:-)

Um, No, you can''t :-)


int main(){

double ** m = my_matrix(4,3); //创建一个4x3矩阵

m [0] [1] = 5.0;

double row2 [] = {1.0,2.0,3.0,4.0};

m [1] = row2; //把它留下来,它不会崩溃

my_free_matrix(m,4,3);

返回0;

}
int main(){
double **m = my_matrix(4,3); // create a 4x3 matrix
m[0][1] = 5.0;
double row2[] = {1.0, 2.0, 3.0, 4.0};
m[1] = row2; // leave this out, and it doesn''t crash
my_free_matrix(m, 4, 3);
return 0;
}



基本上用C语言你需要理解指针及其指向的内容。


m是一个指针。它指向数组的第一个元素。该数组的每个元素

本身就是指向数组第一个元素的指针。


你所做的就是分配你需要的所有空间然后更改

指针,它是顶级数组中的第二个指针。


然后当你去释放矩阵时,发生了两件事情;

a。 my_free_matrix试图在堆栈上释放该数组(它没有像b
那样)

b。 my_free_matrix没有尝试释放你分配的空间(因为

你把指针掉了下来)。


你应该做的就是复制元素第2行进入空间

由m [1]指出。


希望有帮助。


-

Bill Medland

Basically with C you need to understand pointers and what they point to.

m is a pointer. It points to the first element of the array. Each element
of that array is itself a pointer to the first element of an array.

What you did was to allocate all the space you needed and then change the
pointer that was the second pointer in the top level array.

Then when you went to deallocate the matrix two things happened;
a. my_free_matrix tried to release that array on the stack (which it didn''t
like doing)
b. my_free_matrix did NOT try and release the space you allocated (because
you dropped the pointer to it).

What you should have done is copy the elements of row2 into the space
pointed to by m[1].

Hopefully that helps.

--
Bill Medland


这篇关于释放matix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆