动态地分配数组解释 [英] Dynamically allocating array explain

查看:228
本文介绍了动态地分配数组解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是示例code我的老师给我们看了关于如何动态地使用C分配一个数组?。但我不完全理解这一点。这里是code:

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 **测试;是不是只是一个指针的指针?和的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 **测试; 测试是<一个href=\"http://stackoverflow.com/questions/18306935/need-of-pointer-to-pointer/18307020#18307020\">pointer到指针,在code PICE对于int值的矩阵分配内存使用动态malloc函数。

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    

分配持续 K 指针为int的内存(为int * )。因此,假设如果 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 |---►| ?  | ?  | ?  |  ? |
+----+    +----+----+----+----+

我假设地址是四个字节和意味着垃圾值。

温度由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和返回地址分配给温度[I] (位置$ p $的pviously分配的数组)。

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    

注:前pression 温度[I] == *(温度+ I)

   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)如果你是动态分配的内存,你需要释放内存明确,当你的工作与完成(释放动态分配的内存,你不能访问内存后)。步骤来释放内存测试将如下:

for (i = 0; i < k; i++){
    free(test[i]);
}
free(test);

3)这是,如果你想完全分配的二维矩阵分配内存数组的数组的一种方式继续存储所有阵列检查这个答案:的分配内存二维数组中的函数C

4)如果描述可以帮助您想了解的3D分配入住这样的回答:<一href=\"http://stackoverflow.com/questions/16522035/allocate-memory-to-char-in-c/16522223#16522223\">Matrix字符串或/ 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屋!

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