免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 21410 | 回复: 53
打印 上一主题 下一主题

[code] C简单实现动态2维数组 :) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-08 16:33 |只看该作者 |正序浏览
这也是近期编写 libscws 时用到的一个小功能,动态2维数组。

struct xxx **x;
x = (struct xxx **) darray_new(5, 4, sizeof(struct xxx));

...
这里就可以用 x[1][3]  ... x[0][3] ... 来操作了:)
...

darray_free(x);



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. void **darray_new(int row, int col, int size)
  5. {       
  6.         void **arr;

  7.         arr = (void **) malloc(sizeof(void *) * row + size * row * col);
  8.         if (arr != NULL)
  9.         {
  10.                 void *head;

  11.                 head = (void *) arr + sizeof(void *) * row;
  12.                 memset(arr, 0, sizeof(void *) * row + size * row * col);
  13.                 while (row--)               
  14.                         arr[row] = head + size * row * col;               
  15.         }
  16.         return arr;
  17. }

  18. void darray_free(void **arr)
  19. {
  20.         if (arr != NULL)
  21.                 free(arr);       
  22. }

复制代码

论坛徽章:
0
54 [报告]
发表于 2011-08-15 12:34 |只看该作者
学习学习

论坛徽章:
0
53 [报告]
发表于 2011-08-14 22:52 |只看该作者
不错的想法,实际上就是利用地址来进行一次跳转,完成了所谓的array[i][j]=*(*(array+i)+j)的功能。

论坛徽章:
0
52 [报告]
发表于 2008-04-27 21:51 |只看该作者
学习

论坛徽章:
0
51 [报告]
发表于 2008-04-26 22:51 |只看该作者
楼主代码写的不错,赞一个。只分配一次空间就搞定,还用上了结构体。我也帖个测试代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct xxx
{
&nbsp;&nbsp;&nbsp;&nbsp;char name[20];
};

void **darray_new(int row, int col, int size)
{        
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void **arr;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr = (void **) malloc(sizeof(void *) * row + size * row * col);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (arr != NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void *head;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head = (void *) arr + sizeof(void *) * row;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(arr, 0, sizeof(void *) * row + size * row * col);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (row--)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[row] = head + size * row * col;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return arr;
}

void darray_free(void **arr)
{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (arr != NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(arr);
}

int main()
{
&nbsp;&nbsp;&nbsp;&nbsp;int i=0, j=0, r=3, c=4,size=0;

&nbsp;&nbsp;&nbsp;&nbsp;struct xxx **pp;
&nbsp;&nbsp;&nbsp;&nbsp;pp = (struct xxx **)darray_new(r, c, sizeof(struct xxx));
&nbsp;&nbsp;&nbsp;&nbsp;for(i=0; i<r; i++)
&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=0; j<c; j++)
&nbsp;&nbsp;&nbsp;&nbsp;     {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf(pp[i][j].name,"zhoukun %d%d", i, j);
&nbsp;&nbsp;&nbsp;&nbsp;     }
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;for(i=0; i<r; i++)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=0; j<c; j++)
&nbsp;&nbsp;&nbsp;&nbsp;     {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("[%s]\n",pp[i][j].name);
&nbsp;&nbsp;&nbsp;&nbsp;     }
&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;darray_free((void **)pp);
&nbsp;&nbsp;&nbsp;&nbsp;pp = NULL;
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}

论坛徽章:
0
50 [报告]
发表于 2008-04-11 15:27 |只看该作者
模糊中!

论坛徽章:
0
49 [报告]
发表于 2008-04-11 11:23 |只看该作者

回复 #7 ailantian 的帖子

实际上是编译器翻译成动态的申请!

论坛徽章:
0
48 [报告]
发表于 2008-04-11 10:43 |只看该作者
原帖由 hightman 于 2007-6-8 16:33 发表
这也是近期编写 libscws 时用到的一个小功能,动态2维数组。

struct xxx **x;
x = (struct xxx **) darray_new(5, 4, sizeof(struct xxx));

...
这里就可以用 x[1][3]  ... x[0][3] ... 来操作了:)
...

好像释放的不彻底。。
测试代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void **darray_new(int row, int col, int size)
  5. {        
  6.         void **arr;
  7.         arr = (void **) malloc(sizeof(void *) * row + size * row * col);
  8.         if (arr != NULL)
  9.         {
  10.                 void *head;
  11.                 head = (void *) arr + sizeof(void *) * row;
  12.                 memset(arr, 0, sizeof(void *) * row + size * row * col);
  13.                 while (row--)               
  14.                         arr[row] = head + size * row * col;               
  15.         }
  16.         return arr;
  17. }
  18. void darray_free(void **arr)
  19. {
  20.         if (arr != NULL)
  21.                 free(arr);        
  22. }

  23. int
  24. main(void)
  25. {
  26.         char **p;
  27.         p=(char **)darray_new(4,5,1);
  28.         strcpy(p[0],"abc");
  29.         strcpy(p[1],"def");
  30.         strcpy(p[2],"ghi");
  31.         printf("p[0]:%s\tp[1]:%s\tp2[2]:%s\n",p[0],p[1],p[2]);
  32.         printf("&p[0]:%x\t&p[1]:%x\t&p2[2]:%x\n",&p[0],&p[1],&p[2]);
  33.         darray_free((void **)p);
  34.         printf("p[0]:%s\tp[1]:%s\t:p2[2]:%s\n",p[0],p[1],p[2]);
  35. }
  36. 结果:
  37. p[0]:abc        p[1]:def        p2[2]:ghi
  38. &p[0]:8b37008   &p[1]:8b3700c   &p2[2]:8b37010
  39. p[0]:(null)     p[1]:def        :p2[2]:ghi

复制代码

[ 本帖最后由 ruoyisiyu 于 2008-4-11 10:51 编辑 ]

论坛徽章:
0
47 [报告]
发表于 2008-04-11 09:25 |只看该作者
原帖由 It'sGifted 于 2008-4-10 23:31 发表

一直BS掘坟的,不过这次是个例外

论坛徽章:
0
46 [报告]
发表于 2008-04-10 23:31 |只看该作者
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP