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

很多老外公布出来的算法代码常常是在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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值