C++中动态创建二维数组——尴尬的不支持C99的VC

来源:互联网 发布:centos 编译安装lamp 编辑:程序博客网 时间:2024/04/29 16:25

很多老外公布出来的算法代码常常是在Linux下实现的。很多时候会遇到动态数组定义。

举个二维数组的例子:

这是C99[6]标准中新添加的特性[3,4,5]。

C99 gives C programmers the ability to use variable length arrays, which are arrays whose sizes are not known until run time. A variable length array declaration is like a fixed array declaration except that the array size is specified by a non-constant expression. When the declaration is encountered, the size expression is evaluated and the array is created with the indicated length, which must be a positive integer. Once created, variable length array cannot change in length. Elements in the array can be accessed up to the allocated length; accessing elements beyond that length results in undefined behavior. There is no check required for such out-of-range accesses. The array is destroyed when the block containing the declaration completes. Each time the block is started, a new array is allocated.[4]

GCC编译器具有对C99标准比较完善的支持,而VC的编译器部分支持C99,但还不支持这种动态数组定义。

我的测试环境是VS 2008,不过至今VS2010都没有支持:

This version of Visual C++ is not conformant with the C99 standard.[8]

 

Thank you for your comment. There's no plans for VC to support C99.[9]

因此,要想在VC中编译老外的代码,要改写成动态创建的数组。

改法如下:

最后释放:

 

简单例子:

后话:

 

  从MS Visual Studio开发小组的态度来看,近期之内,不会对C99进行完整的支持[7,8,9],这就意味着,可能在Windows下跑老外的Linux算法的时候涉及更多的代码改编。
  因此,自然就要想到,如何让Visual Studio支持C99,我还没有去尝试,不过这里有篇帖子《VC里边怎么用C99》[10],方法是在VS中使用Intel C++编译器,抽时间可以试一下。

 

 

Referrences:

 

[1]Image Segmentation Code,http://cms.brookes.ac.uk/staff/PhilipTorr/Code/seg_page_1.htm

[2]C++中二维数组的动态创建与处理,http://www.programfan.com/blog/article.asp?id=25162

[3]可变长数组在GCC编译器中的实现,http://hi.baidu.com/wch20088082008/blog/item/1a7e09256b53c825d40742a7.html

[4]C语言变长数组之剖析,http://blog.chinaunix.net/space.php?uid=7897183&do=blog&cuid=220612

[5]我很满意C99的这几个特性(2) —— 定义变量和不定长数组,http://hi.baidu.com/zotin/blog/item/ca0f9023972c0f49ac34deb3.html

[6]C99 - Wikipedia, the free encyclopedia,http://en.wikipedia.org/wiki/C99

[7]Visual Studio support for new C / C++ standards?,http://stackoverflow.com/questions/146381/visual-studio-support-for-new-c-c-standards

[8]C Run-Time Libraries,http://msdn.microsoft.com/en-us/library/abx4dbyh.aspx

[9]C99 support ,http://connect.microsoft.com/VisualStudio/feedback/details/653336/c99-support

[10]VC 里边怎么用C99?,http://playmov.cn/post-16.html

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 十个月的小孩不吃饭怎么办 家长要调幼儿园监控怎么办 自己带孩子婆婆生气怎么办 婆婆老是觉得我奶水不够怎么办 1岁小儿特别懒怎么办 17的孩子很懒怎么办 上大班的孩子不愿写字怎么办 快上中班的小朋友不爱学习怎么办 小学生两边肩膀不平应该怎么办 写字右肩膀疼是怎么办 开车久了肩膀疼怎么办 3岁宝宝撕书怎么办 孩子上幼儿园不爱写字怎么办 一年级小孩不爱做作业怎么办 小孩不愿多做作业怎么办 小孩一年级不自觉做作业怎么办 小孩会读不会写怎么办 好多字都不会写怎么办 写作业怎么办才能写快 五周宝宝爱玩不写字怎么办 爱玩手机的小孩怎么办? 一年级学生记不住生字怎么办 一年级小孩记不住生字怎么办 配镜度数高了怎么办 宝宝两岁半不肯坐马桶拉臭臭怎么办 儿子字写得不好 怎么办 小孩不听话不爱读书和写字怎么办 两岁宝宝不愿意穿衣服怎么办 做题粗心不认真怎么办 5岁宝宝不会写字怎么办 四岁宝宝不会写字怎么办 4岁宝宝不写字怎么办 四岁宝宝不写字怎么办 孩子学习粗心计算能力差怎么办 一年级的小朋友不爱看书怎么办 马上要生了害怕怎么办 孩子做题不爱读题怎么办 孩子作业写的慢怎么办 孩子学习不好怎么办我们有绝招 英语不会做题怎么办呢? 小学二年级孩子厌学怎么办