免费注册 查看新帖 |

Chinaunix

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

[C] 不确定成员个数的数组的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-21 11:55 |只看该作者 |倒序浏览
一直说,当数组元素个数不确定的时候,我们可以用动态数组
说数组不支持运行时才确定个数的情况
但是我用下面的程序,编译器也不报错,也能正常运行,这是为什么呢

   int pp;
    scanf("%d", &pp);

    int dd[pp];
    dd[0] = 10;
    dd[1] = 11;

和编译器有关系? 我用的是gcc 2.95

论坛徽章:
0
2 [报告]
发表于 2009-09-21 12:53 |只看该作者
还真能过

[ 本帖最后由 churchmice 于 2009-9-21 12:56 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-09-21 13:08 |只看该作者
查了一下,属于gcc的extension,也是c99标准的一部分
Variable-length automatic arrays are allowed in ISO C99, and as an
extension GCC accepts them in C89 mode and in C++.  (However, GCC's
implementation of variable-length arrays does not yet conform in detail
to the ISO C99 standard.)  These arrays are declared like any other
automatic arrays, but with a length that is not a constant expression.
The storage is allocated at the point of declaration and deallocated
when the brace-level is exited.  For example:

     FILE *
     concat_fopen (char *s1, char *s2, char *mode)
     {
       char str[strlen (s1) + strlen (s2) + 1];
       strcpy (str, s1);
       strcat (str, s2);
       return fopen (str, mode);
     }

Jumping or breaking out of the scope of the array name deallocates the
storage.  Jumping into the scope is not allowed; you get an error
message for it.

info gcc -> C Extension -> Arrays of Variable Length
自己慢慢看吧

论坛徽章:
0
4 [报告]
发表于 2009-09-21 13:36 |只看该作者
C99的新特性, 动态数组

论坛徽章:
0
5 [报告]
发表于 2009-09-21 13:40 |只看该作者
呵呵 之前正好看了Gnu's C扩展,从浏览器的历史记录里找出来了

http://www.ddj.com/cpp/184401956

论坛徽章:
0
6 [报告]
发表于 2009-09-21 15:19 |只看该作者

回复 #3 churchmice 的帖子

谢谢churchmice
那局部变量的动态数组,我们不是就可以不用自己去new了

论坛徽章:
0
7 [报告]
发表于 2009-09-21 17:09 |只看该作者
栈上动态分配,应该相当于alloca

论坛徽章:
0
8 [报告]
发表于 2009-09-21 17:21 |只看该作者
不是所有的编译器都支持,能不这样写,就不要这样写吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP