Dynamically create two-dimensional arrays in C ++ -- awkwardly does not support C99 VC

Source: Internet
Author: User
Tags array definition

Algorithm code published by many foreigners is often implemented in Linux. In many cases, dynamic array definitions are encountered.

Here is an example of a two-dimensional array:

Graph: node_id node [height] [width];

This is c99 [6
] Features newly added to the standard [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 doesn't 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
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
Block containing the Declaration completes. Each time the block is
Started, a new array is allocated. [4]

The GCC compiler supports the c99 standard, while the VC compiler supports c99, but does not support dynamic array definition.

My test environment is vs 2008, but so far vs2010 does not support:

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
]

Therefore, to compile code for foreigners in VC, rewrite the code to a dynamically created array.

The modification method is as follows:

Graph: node_id ** node = new Graph: node_id * [height]; <br/> for (int I = 0; I <peight; ++ I) <br/>{< br/> node [I] = new Graph: node_id [width]; <br/>}< br/>

Last released:

For (int I = 0; I <peight; I ++) <br/> {<br/> delete [] node [I]; <br/>}< br/> delete [] node;

 

Simple Example:

# Include "stdafx. h "<br/> # include" stdio. h "<br/> int _ tmain (int argc, _ TCHAR * argv []) <br/>{< br/> int rows = 3; <br/> int cols = 5; <br/> int ** p2d = new int * [rows]; <br/> for (int I = 0; I <rows; I ++) <br/>{< br/> p2d [I] = new int [cols]; <br/>}< br/> for (int I = 0; I <rows; I ++) <br/>{< br/> for (int j = 0; j <cols; j ++) <br/> {<br/> p2d [I] [j] = I * cols + j; <br/> printf ("% d/t ", p2d [I] [j]); <br/>}< br/> printf ("/n "); <br/>}< br/> for (int I = 0; I <rows; I ++) <br/>{< br/> delete [] p2d [I]; <br/>}< br/> delete [] p2d; <br/> return 0; <br/>}< br/>

After that:

 

According to the attitude of the MS Visual Studio development team, c99 will not be fully supported in the near future [7
, 8
, 9
], Which means that more code adaptation may be involved when running a foreigner's Linux Algorithm in windows.
Therefore, we naturally need to think about how to enable Visual Studio to support c99. I haven't tried it yet, but here is a post titled "How to Use c99 in VC" [10
], The method is to use the Intel C ++ compiler in vs. You can try it out later.

 

 

Referrences:

 


[1] image segmentation code, http://cms.brookes.ac.uk/staff/PhilipTorr/Code/seg_page_1.htm


[2] C ++ 2D array dynamic creation and processing, http://www.programfan.com/blog/article.asp? Id = 25162


[3] Variable Length Array Implementation in GCC compiler, http://hi.baidu.com/wch20088082008/blog/item/1a7e09256b53c825d40742a7.html


[4] Analysis of C language variable length array, http://blog.chinaunix.net/space.php? Uid = 7897183 & do = blog & cuid = 220612


[5] I'm very satisfied with these features of C99 (2) -- defining variables and uncertain length arrays, 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] How to Use C99 in VC ?, Http://playmov.cn/post-16.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.