如何分配具有内存的二维数组? 如何使用它来访问行和列? 举个例子
我创建了一个2 d数组,其内容如下
int i,j,lx,ly;// lx,ly are the row and column respectively double** a; a=(double**) malloc((lx+2)*sizeof(double)); a[0]= (double*) malloc((lx+2)*(ly+2)* sizeof(double)); assert(a[0]); for(i=1;i<lx+2;i++) { a[i]=a[i-1]+i*(ly+2); }
//我为这个数组中的所有元素分配值0,如下所示
for(i=0;i<(lx+2)*(ly+2);i++) { a[i]=0; }
//我打印下面的所有元素
for(i=0;i<(lx+2)*(ly+2);i++) { printf("position %d values %d\n",i,a[i]); }
//当我看到输出时,它显示我在一个特定位置的垃圾值13.我无法弄清楚..还请告诉我如何访问像Eg这样的行和列来访问第7列第0行和第5行以lx表示第6行,如我的代码所示
你的方法肯定是朝着正确的方向发展。
我认为这:
a=(double**) malloc((lx+2)*sizeof(double));
通常是:
a = malloc(lx * sizeof(double *));
然后没有邻接要求,这个:
a[0]= (double*) malloc((lx+2)*(ly+2)* sizeof(double));
在大多数程序中看起来像:
a[0] = malloc(ly * sizeof(double));
最后,最后一行需要在一个循环中,为每个a[i]
分配它自己的malloc’ed空间。
但是,这不会创建连续的内存。 为此,您需要进行大量分配,然后将其划分为行向量。 所以,而不是循环中的第二个malloc,可能是这样的:
double *t = malloc(lx * ly * sizeof(double)); for (i = 0; i < lx; ++i) a[i] = t + i * ly;
把它们放在一起:
#include #include void arrayDemo(int lx, int ly) { double **a; int i, j; a = malloc(lx * sizeof(double *)); double *t = malloc(lx * ly * sizeof(double)); for(i = 0; i < lx; ++i) a[i] = t + i * ly; for(i = 0; i < lx; ++i) for(j = 0; j < ly; ++j) a[i][j] = i*100 + j; for(i = 0; i < lx; ++i) { for(j = 0; j < ly; ++j) printf(" %4.0f", a[i][j]); printf("\n"); } } int main(int ac, char **av) { arrayDemo(atoi(av[1]), atoi(av[2])); return 0; } $ cc -Wall all.c $ ./a.out 4 7 0 1 2 3 4 5 6 100 101 102 103 104 105 106 200 201 202 203 204 205 206 300 301 302 303 304 305 306
此代码分配一个10乘5的连续内存块,使用递增的双精度对其进行初始化,然后打印由x和y索引的值:
#include "2d.h" int main(void){ unsigned int x,y; const unsigned int width = 10; const unsigned int height = 5; //we need an index into the x of the array double * index[width]; //need the memory to store the doubles unsigned int memorySizeInDoubles = width * height; double * memory = malloc(memorySizeInDoubles * sizeof(double)); //initialize the memory with incrementing values for(x = 0; x < memorySizeInDoubles; ++x){ memory[x] = (double) x; } //initialize the index into the memory for(x = 0; x < width; ++x){ index[x] = memory + height * x; } //print out how we did for(x = 0; x < width; ++x){ for(y = 0; y < height; ++y){ printf("[%u, %u]: Value = %f\n", x, y, index[x][y]); } } free(memory); return 0; }
2d.h文件应包含以下行:
#include #include int main(void);
注意:创建的内存仅对某些定义是连续的。 内存在逻辑上是连续的,但不一定是物理上连续的。 例如,如果此内存用于设备驱动程序,则malloc将不起作用。
要么创建单个维度数组
double my_array = malloc(sizeof(double) * size_x * sizeof(double) * size_y);
您将访问
(得到位置x = 28,y = 12)
my_array[12 * size_x + 28];
或者像你一样创建一个二维数组,但是你可以使用它来访问它
double **my_array = (double**) malloc(15 * sizeof(double)); for(int i = 0 ; i < 25; i++) { my_array[i] = (double*) malloc(30 * sizeof(double)); for (int j = 0 ; j < 12; j++) { my_array[i][j] = 1.2; } } double my_double = my_array[12][28];
在C中,要有一块连续的内存,你需要一个malloc()
,或者有一个静态分配的数组。 由于您需要动态内存,因此需要malloc()
。 由于您需要一切都是连续的,因此您只需要拨打一次电话即可。
现在,这个电话应该是什么样的? 如果我理解正确,你需要lx
次ly
值,每个值的大小sizeof(double)
,所以你需要分配lx*ly*sizeof(double)
字节。
题外话:我更喜欢编写我的malloc()
调用,如下所示:
#include /* for malloc's prototype */ T *pt; /* for any type T */ size_t n; /* need n objects of type T */ pt = malloc(n * sizeof *pt);
使用sizeof
sizeof *pt
而不是sizeof(T)
提供了一个优势,即如果pt
的类型发生变化,则无需更改malloc()
调用。 不转换malloc()
的结果很好,因为整个malloc()
调用是类型无关的,并且更容易键入和读取。 但请务必#include
。
因此,要为n
double
s分配空间,您可以:
double *pd = malloc(n * sizeof *pd); if (pd != NULL) { /* malloc succeeded */ } else { /* malloc failed */ }
现在,在分配内存之后,您需要能够对其进行索引。 假设你有lx == 2
和ly == 3
。 你的记忆如下:
+---+---+---+---+---+---+ pd: | 0 | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+
pd[0]
, pd[1]
和pd[2]
是对应于第一行的double
值, pd[3]
到pd[6]
是对应于第二行的double
值。 您应该能够概括此观察结果,将给定的x,y
索引对转换为正确索引到pd
数组中的一个数字。