动态分配数组说明 [英] Dynamically allocating array explain
问题描述
这是我的老师向我们展示的关于如何在 C 中动态分配数组?"的示例代码.但我并不完全理解这一点.代码如下:
This is sample code my teacher showed us about "How to dynamically allocate an array in C?". But I don't fully understand this. Here is the code:
int k;
int** test;
printf("Enter a value for k: ");
scanf("%d", &k);
test = (int **)malloc(k * sizeof(int*));
for (i = 0; i < k; i++) {
test[i] = (int*)malloc(k * sizeof(int)); //Initialize all the values
}
我认为在 C 中,要定义一个数组,您必须将 []
放在名称后面,那么 int** test
到底是什么;它不只是一个指向指针的指针吗?而 malloc()
行也让我很困惑.....
I thought in C, to define an array you had to put the []
after the name, so what exactly is int** test
; isn't it just a pointer to a pointer? And the malloc()
line is also really confusing me.....
推荐答案
根据声明int** test;
,test
为指向指针的指针,以及使用 malloc 函数为 int 值矩阵动态分配内存的代码.
According to declaration int** test;
, test
is pointer to pointer, and the code pice allocating memory for a matrix of int values dynamically using malloc function.
声明:
test = (int **)malloc(k * sizeof(int*));
// ^^------^^-------
// allocate for k int* values
为指向 int (int*
) 的 k
指针分配继续内存.所以假设如果 k = 4
那么你会得到类似的结果:
Allocate continue memory for k
pointers to int (int*
). So suppose if k = 4
then you gets something like:
temp 343 347 351 355
+----+ +----+----+----+----+
|343 |---►| ? | ? | ? | ? |
+----+ +----+----+----+----+
我假设地址是四个字节,?
表示垃圾值.
I am assuming addresses are of four bytes and ?
means garbage values.
temp
变量由 malloc 分配返回地址,malloc 分配大小为 k * sizeof(int**)
的连续内存块,在我的示例中为 16 字节.
temp
variable assigned returned address by malloc, malloc allocates continues memory blocks of size = k * sizeof(int**)
that is in my example = 16 bytes.
在 for 循环中,您为 k
int 分配内存并将返回的地址分配给 temp[i]
(先前分配的数组的位置).
In the for loop you allocate memory for k
int and assign returned address to temp[i]
(location of previously allocated array).
test[i] = (int*)malloc(k * sizeof(int)); //Initialize all the values
// ^^-----^^----------
// allocate for k int values
注意:表达式temp[i]
== *(temp + i)
.因此,在每次迭代的 for 循环中,您为 k 个 int 值数组分配内存,如下所示:
Note: the expression temp[i]
== *(temp + i)
. So in for loop in each iterations you allocate memory for an array of k int values that looks something like below:
First malloc For loop
--------------- ------------------
temp
+-----+
| 343 |--+
+-----+ |
▼ 201 205 209 213
+--------+ +-----+-----+-----+-----+
343 | |= *(temp + 0) | ? | ? | ? | ? | //for i = 0
|temp[0] |-------| +-----+-----+-----+-----+
| 201 | +-----------▲
+--------+ 502 506 510 514
| | +-----+-----+-----+-----+
347 |temp[1] |= *(temp + 1) | ? | ? | ? | ? | //for i = 1
| 502 |-------| +-----+-----+-----+-----+
+--------+ +-----------▲
| | 43 48 52 56
351 | 43 | +-----+-----+-----+-----+
|temp[2] |= *(temp + 2) | ? | ? | ? | ? | //for i = 2
| |-------| +-----+-----+-----+-----+
+--------+ +-----------▲
355 | |
| 9002 | 9002 9006 9010 9014
|temp[3] | +-----+-----+-----+-----+
| |= *(temp + 3) | ? | ? | ? | ? | //for i = 3
+--------+ | +-----+-----+-----+-----+
+-----------▲
再次 ?
表示垃圾值.
补充要点:
1) 您正在通过 malloc 转换返回的地址,但在 C 中您应该避免它.阅读 我是否要转换 malloc 的结果?做如下:
1) You are casting returned address by malloc but in C you should avoid it. Read Do I cast the result of malloc? just do as follows:
test = malloc(k* sizeof(int*));
for (i = 0; i < k; i++){
test[i] = malloc(k * sizeof(int));
}
2) 如果您动态分配内存,则需要在完成工作后明确释放内存(在释放动态分配的内存后,您无法访问该内存).为 test
释放内存的步骤如下:
2) If you are allocating memory dynamically, you need to free memory explicitly when your work done with that (after freeing dynamically allocated memory you can't access that memory). Steps to free memory for test
will be as follows:
for (i = 0; i < k; i++){
free(test[i]);
}
free(test);
3) 如果您想为所有数组分配完全连续的内存,这是为 2D 矩阵作为数组分配内存的一种方法,请查看此答案:在函数C中分配内存二维数组
3) This is one way to allocate memory for 2D matrix as array of arrays if you wants to allocate completely continues memory for all arrays check this answer: Allocate memory 2d array in function C
4) 如果描述有帮助并且您想学习 3D 分配,请查看此答案:字符串或/3D 字符数组的矩阵
4) If the description helps and you want to learn for 3D allocation Check this answer: Matrix of String or/ 3D char array
这篇关于动态分配数组说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!