内存腐败问题 [英] Memory Corruption Problem

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

问题描述




受近期帖子问题的启发?我开始写一个简单的

矩阵库。我的问题是,当我运行下面的程序时,我得到了

意想不到的结果。


$ cat test.c

#include < stdio.h>

#include< stdlib.h>


#define SIZE 2


struct matrix

{

int rows,cols;

double ** at;

};

typedef struct matrix Matrix;


Matrix * new_matrix(int rows,int cols,const double * elements)

{

Matrix * res;

int j,k;


res = malloc(sizeof(Matrix));

res-> rows = rows;

res-> cols = cols;

res-> at = malloc(rows * sizeof(double *));

for(j = 0; j< rows; j ++){

res-> at [j] = malloc(cols * sizeof(double *));

}

if(elements!= NULL){

for(j = 0; j< rows; j ++){

for(k = 0; k< cols; k ++){

res-> at [j] [k] = elements [j * cols + k];

}

}

}

返回res;

}

int main(无效)

{

矩阵* A,* B;

加倍[SIZE * SIZE] = {1.0,2.0,3.0,4.0};


A = new_matrix(SIZE,SIZE,a);

printf("%f\ n",A-> at [1] [1]);

B = new_matrix(SIZE,SIZE,a);

printf("%f \ n",A-> at [1] [1]);

返回0;

}

$ gcc -ansi -pedantic -std = c89 -Wall -g test.c module.c -o test

$ ./test

4.000000

0.000000


我希望输出为


4.000000

4.000000


任何线索?

问候,


八月


-

我是ILOVEGNU签名病毒。只需将我复制到您的

签名即可。此电子邮件受到GNU

通用公共许可证条款的影响。

Hi,

Inspired by the recent post by "questions?" I started to write a simple
matrix library. My problem is that when I run the program below I get
unexpected results.

$ cat test.c
#include <stdio.h>
#include <stdlib.h>

#define SIZE 2

struct matrix
{
int rows, cols;
double **at;
};
typedef struct matrix Matrix;

Matrix *new_matrix(int rows, int cols, const double *elements)
{
Matrix *res;
int j, k;

res = malloc(sizeof (Matrix));
res->rows = rows;
res->cols = cols;
res->at = malloc(rows * sizeof (double *));
for (j = 0; j < rows; j++) {
res->at[j] = malloc(cols * sizeof (double *));
}
if (elements != NULL) {
for (j = 0; j < rows; j++) {
for (k = 0; k < cols; k++) {
res->at[j][k] = elements[j * cols + k];
}
}
}
return res;
}
int main(void)
{
Matrix *A, *B;
double a[SIZE * SIZE] = {1.0, 2.0, 3.0, 4.0};

A = new_matrix(SIZE, SIZE, a);
printf("%f\n", A->at[1][1]);
B = new_matrix(SIZE, SIZE, a);
printf("%f\n", A->at[1][1]);
return 0;
}
$ gcc -ansi -pedantic -std=c89 -Wall -g test.c module.c -o test
$ ./test
4.000000
0.000000

I expect the output to be

4.000000
4.000000

Any clues?
Regards,

August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.

推荐答案

cat test.c
#include< stdio.h>

#include< stdlib.h>


#define SIZE 2
< br $>
struct matrix

{

int rows,cols;

double ** at;

};

typedef struct matrix Matrix;


Matrix * new_matrix(int rows,int cols,const double * elements)

{

Matrix * res;

int j,k;


res = malloc(sizeof(Matrix));

res-> rows = rows;

res-> cols = cols;

res-> at = malloc(rows * sizeof(double *));

for(j = 0; j< rows; j ++){

res-> at [j] = malloc(cols * sizeof(double *));

}

if(elements!= NULL){

for(j = 0; j< rows ; j ++){

for(k = 0; k< cols; k ++){

res-> at [j] [k] = elements [j * cols + k];

}

}

}

返回res;

}

int main(无效)

{

矩阵* A,* B;

双倍a [SIZE * SIZE] = {1.0,2.0,3.0,4.0};


A = new_matrix(SIZE,SIZE,a);

printf(" ;%f \ n",A-> at [1] [1]);

B = new_matrix(SIZE,SIZE,a);

printf( "%f\ n,A-> at [1] [1]);

返回0;

}
cat test.c
#include <stdio.h>
#include <stdlib.h>

#define SIZE 2

struct matrix
{
int rows, cols;
double **at;
};
typedef struct matrix Matrix;

Matrix *new_matrix(int rows, int cols, const double *elements)
{
Matrix *res;
int j, k;

res = malloc(sizeof (Matrix));
res->rows = rows;
res->cols = cols;
res->at = malloc(rows * sizeof (double *));
for (j = 0; j < rows; j++) {
res->at[j] = malloc(cols * sizeof (double *));
}
if (elements != NULL) {
for (j = 0; j < rows; j++) {
for (k = 0; k < cols; k++) {
res->at[j][k] = elements[j * cols + k];
}
}
}
return res;
}
int main(void)
{
Matrix *A, *B;
double a[SIZE * SIZE] = {1.0, 2.0, 3.0, 4.0};

A = new_matrix(SIZE, SIZE, a);
printf("%f\n", A->at[1][1]);
B = new_matrix(SIZE, SIZE, a);
printf("%f\n", A->at[1][1]);
return 0;
}


gcc -ansi -pedantic -std = c89 -Wall -g test.c module.c -o test
gcc -ansi -pedantic -std=c89 -Wall -g test.c module.c -o test


./ test

4.000000

0.000000


我希望输出为


4.000000

4.000000


任何线索?

问候,


八月

-

我是ILOVEGNU签名病毒。只需将我复制到您的

签名即可。此电子邮件受到GNU

通用公共许可证条款的影响。
./test
4.000000
0.000000

I expect the output to be

4.000000
4.000000

Any clues?
Regards,

August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.


这篇关于内存腐败问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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