C 如何分配具有连续内存的二维阵列?如何使用它访问行和列?给我举个例子
我创建了一个二维数组,如下所示C 如何分配具有连续内存的二维阵列?如何使用它访问行和列?给我举个例子,c,arrays,dynamic-allocation,C,Arrays,Dynamic Allocation,我创建了一个二维数组,如下所示 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
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);
}
inti,j,lx,ly;//lx、ly分别为行和列
双**a;
a=(双**)malloc((lx+2)*sizeof(双));
a[0]=(双*)malloc((lx+2)*(ly+2)*sizeof(双));
断言(a[0]);
对于(i=1;i您可以创建一个一维数组
double my_array = malloc(sizeof(double) * size_x * sizeof(double) * size_y);
您将通过
(获取位置x=28,y=12)
或者像您一样创建二维阵列,但使用
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];
double**my_数组=(double**)malloc(15*sizeof(double));
对于(int i=0;i<25;i++)
{
my_数组[i]=(双精度*)malloc(30*sizeof(双精度));
对于(int j=0;j<12;j++)
{
my_数组[i][j]=1.2;
}
}
double my_double=我的_数组[12][28];
你的方法肯定朝着正确的方向前进
我认为:
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;
double*t=malloc(lx*ly*sizeof(double));
对于(i=0;i
总而言之:
#include <stdio.h>
#include <stdlib.h>
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
#包括
#包括
void arrayDemo(int lx,int ly)
{
双**a;
int i,j;
a=malloc(lx*sizeof(double*);
double*t=malloc(lx*ly*sizeof(double));
对于(i=0;i
此代码分配一个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”
内部主(空){
无符号整数x,y;
常量无符号整数宽度=10;
常数无符号整数高度=5;
//我们需要一个数组x的索引
双*索引[宽度];
//需要内存来存储双工
无符号int-memorySizeInDoubles=宽度*高度;
double*memory=malloc(memorySizeInDoubles*sizeof(double));
//用递增的值初始化内存
对于(x=0;x
2d.h文件应包含以下行:
#include <stdio.h>
#include <stdlib.h>
int main(void);
#包括
#包括
int main(无效);
注意:创建的内存仅对某些定义是连续的。内存在逻辑上是连续的,但在物理上不一定是连续的。例如,如果此内存用于设备驱动程序,malloc将无法工作。在C中,要有一块连续内存,需要一块malloc()
,或具有静态分配的数组。由于需要动态内存,因此需要malloc()
。由于需要所有内容都是连续的,因此只需对其进行一次调用
现在,调用应该是什么样子?如果我理解正确,您需要lx
timesly
值,每个值的大小sizeof(double)
,因此需要分配lx*ly*sizeof(double)
字节
题外话:我更喜欢按如下方式编写我的malloc()
调用:
#include <stdlib.h> /* for malloc's prototype */
T *pt; /* for any type T */
size_t n; /* need n objects of type T */
pt = malloc(n * sizeof *pt);
现在,分配内存后,您需要能够对其进行索引。假设您有lx==2
和ly==3
。您的内存如下所示:
+---+---+---+---+---+---+
pd: | 0 | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+
pd[0]
、pd[1]
和pd[2]
是对应于第一行的pd[3]
到pd[6]的double
值
是与第二行相对应的double
值。您应该能够概括此观察结果,将给定的x,y
索引对转换为一个数字,该数字可以正确索引到pd
数组中。这是什么?a[0]=(double*)malloc((lx+2)*(ly+2)*sizeof(double));这不是初始化数组的第二维度的方式。我正在标记此c
,如果我错了,请更改它。此网站适用于所有编程活动,大多数读者根本不使用c。请帮助那些可以帮助您找到有意义标记的问题的人。这似乎像是家庭作业。您能解释lx和ly吗?他们会的似乎没有初始化。嗨,约翰,这是一个家庭作业,但这是其中的一部分。我需要学习如何使用连续的2D数组来完成整个作业。嗨,使用2个malloc不会给我一个连续内存,对吗?我期望的是一个连续内存…因此有任何机制可以做到这一点吗?对,有时是这样ds增量回答,抱歉延迟。:-)最后一行工作,因为首先计算i*lx
,然后变为指针+int,这导致int按对象大小缩放。您好,第二个代码看起来很酷。你能详细说明一下吗?这就是我所理解的。。。我只是在数组中的每一行上做一个[I]点,对吗?现在我需要做什么才能访问相同的内容,在行和列中填充值。提前感谢您的快速回复。还有什么
#include <stdio.h>
#include <stdlib.h>
int main(void);
#include <stdlib.h> /* for malloc's prototype */
T *pt; /* for any type T */
size_t n; /* need n objects of type T */
pt = malloc(n * sizeof *pt);
double *pd = malloc(n * sizeof *pd);
if (pd != NULL) {
/* malloc succeeded */
} else {
/* malloc failed */
}
+---+---+---+---+---+---+
pd: | 0 | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+