本文介绍了使用malloc失败的2-D矩阵分配正确的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我是比较新的c和想创建使用malloc两张2-D阵列。有人告诉我,这种方法比创建阵列通过一个for循环的指针数组(大型阵列)计算更高效。

  INT I,J;
双**的pNOW,** PNext,* Array2D1,* Array2D2;//分配内存
=的pNOW(双**)的malloc(3 * sizeof的(双*));
PNext =(双**)的malloc(3 * sizeof的(双*));
Array2D1 =(双*)malloc的(5 * sizeof的(双));
Array2D2 =(双*)malloc的(5 * sizeof的(双));//创建2维
对于(I = 0; I&下; 3;我+ +)
{
    的pNOW [I] = Array2D1 + I * 5;
    PNext [I] = Array2D2 + I * 5;
};
//定义元素的值
对于(I = 0; I&下; 3;我+ +)
{
    为(J = 0; J&小于5; J ++)
    {
        的pNOW [I] [J] = 10 *(I + J)。
        。PNext [I] [J] = 1000 *(I + J);
    };
};
//输出两个矩阵并排侧。
对于(I = 0; I&下; 3;我+ +)
{
    为(J = 0; J&小于5; J ++)
    {        的printf(%6lg的pNOW [I] [J]);
        如果(J == 4)
        {
            的printf(|);
        };
    };
    为(J = 0; J&小于5; J ++)
    {        的printf(%6lg,PNext [I] [J]);
        如果(J == 4)
        {
            的printf(\\ n);
        };
    };
};

我的问题是,第一矩阵(的pNOW)原来是我所期望的,但由于某种原因一半PNext值是那些中的pNOW,我不能为我的生命弄清楚为什么它是这样做呢?我显然缺少的东西..另外,我没有什么过于明显的Array2D1 + I * 5在做什么,怎么这使得一个的pNOW 2-D数组?

任何帮助将是非常美联社preciated。
谢谢你。

P.S。这是我得到的输出,所以你可以看到我的意思是:

  0 10 20 30 40 | 20 30 40 50 20
10 20 30 40 50 | 30 40 50 60 5000
20 30 40 50 60 | 2000 3000 4000 5000 6000


解决方案

在C你不投mallocs的结果,所以你的malloc行应该读

  =的pNOW的malloc(3 * sizeof的(双*));

您的问题是你没有实际分配在 Array2D1 足够的内存和 Array2D2 。当您在阵列中移动过去的第一个行你要超越你分配的内存!所以,你在未定义行为的领土。对你来说,它看起来像你的两个矩阵步骤都在彼此(虽然我的测试只是抛出一个错误)。您可以通过两种方式解决这个问题:

指定您在的malloc矩阵的全尺寸,做你所做的:

  Array2D1 =的malloc(15 * sizeof的(双));
Array2D2 =的malloc(15 * sizeof的(双));

或者malloc的每一行的for循环:

 为(i = 0;我3;;我++){
    的pNOW [I] =的malloc(5 * sizeof的(双));
    PNext [I] =的malloc(5 * sizeof的(双));
}

编辑:在每个例子释放的话题

有关的第一个例子,释放是直线前进

 自由(的pNOW);
免费(PNext);
免费(Array2D1);
免费(Array2D2);

对于第二个,您必须通过每一行和免费独立

迭代

 为(i = 0;我3;;我++){
    免费(的pNOW [I]);
    免费(PNext [I]);
}

EDIT2:实际上,如果你要硬code你的行和列以pre-处理器宏,没有理由在所有MALLOC。你可以简单地做到这一点:

 的#define ROW 3
#定义COL 5双的pNOW [ROW] [COL],PNext [ROW] [COL];

EDIT3:至于什么 Array2D1 + I * 5 是干什么的,的pNOW 是一个指针数组, Array2D1 是一个指针。通过添加我* 5 你被我* 5 (即说:给我一个指针递增指针到是记忆我* 5 Array2D1 )。所以,你填写<$ C $远一倍C>的pNOW 的指针的适当大小的内存块的开始为你的行。

sorry, I'm relatively new to c and am trying to create two 2-D arrays using malloc. I was told that this method is computationally more efficient than creating a pointer array of arrays through a for loop (for large arrays).

int i, j;
double **PNow, **PNext, *Array2D1, *Array2D2;

//Allocate memory
PNow = (double**)malloc(3 * sizeof(double*));
PNext = (double**)malloc(3 * sizeof(double*));
Array2D1 = (double*)malloc(5 * sizeof(double));
Array2D2 = (double*)malloc(5 * sizeof(double));

//Create 2-Dimensionality
for(i = 0; i < 3; i++)
{
    PNow[i] = Array2D1 + i * 5;
    PNext[i] = Array2D2 + i * 5;
};


//Define Element Values
for(i = 0; i < 3; i++)
{
    for(j = 0; j < 5; j++)
    {
        PNow[i][j] = 10.*(i + j);
        PNext[i][j] = 1000.*(i + j);
    };
};


//Output two matrices side-by-side.
for(i = 0; i < 3; i++)
{
    for(j = 0; j < 5; j++)
    {

        printf("%6lg", PNow[i][j]);
        if(j == 4)
        {
            printf("|");
        };
    };
    for(j = 0; j < 5; j++)
    {

        printf("%6lg", PNext[i][j]);
        if(j == 4)
        {
            printf("\n");
        };
    };
};

My problem is that the first matrix (PNow) turns out as I would expect, but for some reason half of the values in PNext are those of PNow, and I can't for the life of me figure out why it is doing this? I'm obviously missing something.. Also I am not overly clear on what "Array2D1 + i*5" is doing and how this makes PNow a 2-D array?

Any help would be really appreciated.Thank you.

P.S. This is the output that I am getting, so you can see what I mean:

 0    10    20    30    40|    20    30    40    50    20
10    20    30    40    50|    30    40    50    60  5000
20    30    40    50    60|  2000  3000  4000  5000  6000
解决方案

In C you don't cast the result of mallocs, so your malloc lines should read

PNow = malloc(3*sizeof(double*));

Your problem is you're not actually allocating enough memory in Array2D1 and Array2D2. When you move past the first "row" in your array you're getting beyond your allocated memory! So you're in undefined behavior territory. In your case, it looks like your two matrices step all over each other (though my test just throws an error). You can solve this in two ways:

Specify the full size of your matrix in the malloc and do as you did:

Array2D1 = malloc(15*sizeof(double));
Array2D2 = malloc(15*sizeof(double)); 

Or malloc each line in your for loop:

for(i=0; i<3; i++){
    PNow[i] = malloc(5*sizeof(double));
    PNext[i] = malloc(5*sizeof(double));
}

Edit: On the topic of freeing in each example

For the first example, the freeing is straight forward

free(PNow);
free(PNext);
free(Array2D1);
free(Array2D2);

For the second, you must iterate through each line and free individually

for (i = 0; i < 3; i++) {
    free(PNow[i]);
    free(PNext[i]);
}

Edit2: Realistically, if you're going to hardcode your rows and columns in with a pre-processor macro, there's no reason to malloc at all. You can simply do this:

#define ROW 3
#define COL 5

double PNow[ROW][COL], PNext[ROW][COL];

Edit3: As for what Array2D1 + i * 5 is doing, PNow is an array of pointers, and Array2D1 is a pointer. By adding i * 5 you're incrementing the pointer by i * 5 (i.e., saying "give me a pointer to the memory that is i * 5 doubles away from Array2D1). So, you're filling PNow with pointers to the starts of appropriately sized memory chunks for your rows.

这篇关于使用malloc失败的2-D矩阵分配正确的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 16:48