{"id":195148,"date":"2013-09-25T14:30:03","date_gmt":"2013-09-25T10:30:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=195148"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=195148","title":{"rendered":"<span class=\"post_title\">GCC \u0438 Varable-Length Arrays<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t\u041e\u0434\u043d\u043e\u0433\u0440\u0443\u043f\u043f\u043d\u0438\u043a \u043f\u0440\u0438\u0441\u043b\u0430\u043b C++-\u043a\u043e\u0434 \u0441 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u0438 \u0432\u044b\u0432\u043e\u0434 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0432\u043e\u0434\u0443. \u041d\u043e \u0431\u043e\u043b\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043c\u0435\u043d\u044f \u0443\u0434\u0438\u0432\u0438\u043b\u0430 \u043d\u0435 \u0435\u0433\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u0430 \u0442\u043e, \u0447\u0442\u043e \u0432 \u043a\u043e\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0441\u044f \u043d\u0430 \u0441\u0442\u0435\u043a\u0435 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0439 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0434\u043b\u0438\u043d\u043e\u0439. \u041f\u043e\u043c\u043d\u0438\u0442\u0441\u044f, \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u044f\u0437\u044b\u043a\u0430 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u044d\u0442\u0438\u043c \u0438 \u0443\u044f\u0441\u043d\u0438\u043b, \u0447\u0442\u043e \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f. \u0421\u0442\u0440\u0430\u043d\u043d\u043e, \u043d\u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043e\u0434\u043d\u043e\u0433\u0440\u0443\u043f\u043f\u043d\u0438\u043a\u0430 \u043d\u0435 \u0432\u044b\u0434\u0430\u043b\u2026<\/p>\n<p>  \u0413\u0434\u0435-\u0442\u043e \u0447\u0438\u0442\u0430\u043b, \u0447\u0442\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442 C++14, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0442\u0440\u044e\u043a. \u041d\u043e \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0433\u043e\u0432\u043e\u0440\u0438\u0442, \u0447\u0442\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c constant expression. \u042d\u0442\u043e \u0435\u0449\u0435 \u0440\u0430\u0437 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441 C99, \u0433\u0434\u0435 VLAs \u0434\u0430\u0432\u043d\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u042f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e GCC 4.8.1, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430 \u043d\u0435\u043c \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c:  <\/p>\n<pre><code class=\"cpp\">void foo( int n) {     int array[n]; } <\/code><\/pre>\n<p>  \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441 \u043e\u043f\u0446\u0438\u0435\u0439 -pedantic \u0447\u0435\u0441\u0442\u043d\u043e \u0432\u044b\u0434\u0430\u043b:  <\/p>\n<blockquote><p>warning: ISO C++ forbids variable length array \u2018array\u2019.<\/p><\/blockquote>\n<p>  \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, VLAs \u0434\u043b\u044f C++ \u0432 gcc \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f <a href=\"http:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Variable-Length.html\">\u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c<\/a>.<\/p>\n<p>  \u0421\u0430\u043c\u043e\u0435 \u0432\u0430\u0436\u043d\u043e\u0435, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 sizeof \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435, \u0430 \u043d\u0435 \u0432 \u043a\u043e\u043c\u043f\u0430\u0439\u043b\u0442\u0430\u0439\u043c\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u0438\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c-\u043d\u0435\u0442\u0438\u043f\u043e\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0430\u043a\u043e\u0433\u043e sizeof( vla ):  <\/p>\n<pre><code class=\"cpp\">template &lt;int p&gt; class A { }; void foo(int length) {     int const_lenght_array[7];     int variable_length_array[length];     A&lt;sizeof(const_lenght_array)&gt; a1; \/\/OK     A&lt;sizeof(variable_length_array)&gt; a2; \/\/ERROR } <\/code><\/pre>\n<p>  \u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435 \u0441\u043b\u0430\u0432\u043d\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442, \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435 \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430:  <\/p>\n<blockquote><p>error: \u2018(unsigned int)((((sizetype)(((ssizetype)length) + -1)) + 1u) * 4u)\u2019 is not a constant expression<\/p><\/blockquote>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u0438 typedef:  <\/p>\n<pre><code class=\"cpp\">int (*p)[length] = &variable_length_array; typedef int variable_int_array_t [length]; <\/code><\/pre>\n<p>  \u0411\u043e\u043b\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e, GNU-\u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b, \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0435\u043d\u043e \u0434\u0430\u0436\u0435 \u0434\u043b\u044f new, \u0433\u0434\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0434\u043b\u0438\u043d\u0443:  <\/p>\n<pre><code class=\"cpp\">void bar(int a, int b) {     int auto_array[a][b]; \/\/OK     int (*dynamic_array)[b] = new int[a][b]; \/\/ERROR  } <\/code><\/pre>\n<p>  \u0425\u043e\u0440\u043e\u0448\u0430\u044f \u043d\u043e\u0432\u043e\u0441\u0442\u044c, \u0447\u0442\u043e \u0432 C++14 \u0431\u0443\u0434\u0443\u0442 \u0432\u0432\u0435\u0434\u0435\u043d\u044b VLAs, \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043e \u043c\u043d\u043e\u0433\u043e\u043c \u043d\u0435 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0435 c VLAs \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0438 C99. \u041f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435, sizeof \u0438 typedef\/using(c++11) \u0434\u043b\u044f \u043d\u0438\u0445 \u0431\u0443\u0434\u0435\u0442 ill-formed. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0435 \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043d\u0430 \u0433\u0440\u0430\u0431\u043b\u0438, \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u043e\u0442 sizeof \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u043f\u0430\u0439\u043b\u0442\u0430\u0439\u043c-\u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f. \u0412\u043e\u0442 <a href=\"http:\/\/gcc.gnu.org\/ml\/gcc-patches\/2013-05\/msg00445.html\">\u0441\u0441\u044b\u043b\u043a\u0430<\/a> \u043d\u0430 \u043f\u0430\u0442\u0447 \u0434\u043b\u044f gcc.<\/p>\n<p>  \u042f \u0437\u043d\u0430\u044e, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u0447\u0438\u0442\u0430\u044e\u0442, \u0447\u0442\u043e VLAs \u0432 C++ \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c\u0438: \u0434\u043b\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0446\u0435\u043b\u0435\u0439 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 vector, \u0438 \u043d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0433\u0434\u0435 \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0446\u0435\u043b\u044c\u044e \u0441\u0442\u0430\u0442\u044c\u0438 \u0441\u0442\u0430\u0432\u0438\u043b\u043e\u0441\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0443 gcc, \u0447\u0442\u043e\u0431\u044b \u0432\u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0438 \u043d\u0435 \u0443\u0434\u0438\u0432\u0438\u0442\u044c\u0441\u044f.<br \/>  \u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2013\/n3639.html\">\u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/a> \u043e VLAs \u0432 open-std. \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/195148\/\"> http:\/\/habrahabr.ru\/post\/195148\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t\u041e\u0434\u043d\u043e\u0433\u0440\u0443\u043f\u043f\u043d\u0438\u043a \u043f\u0440\u0438\u0441\u043b\u0430\u043b C++-\u043a\u043e\u0434 \u0441 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u0438 \u0432\u044b\u0432\u043e\u0434 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0432\u043e\u0434\u0443. \u041d\u043e \u0431\u043e\u043b\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043c\u0435\u043d\u044f \u0443\u0434\u0438\u0432\u0438\u043b\u0430 \u043d\u0435 \u0435\u0433\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u0430 \u0442\u043e, \u0447\u0442\u043e \u0432 \u043a\u043e\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0441\u044f \u043d\u0430 \u0441\u0442\u0435\u043a\u0435 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0439 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0434\u043b\u0438\u043d\u043e\u0439. \u041f\u043e\u043c\u043d\u0438\u0442\u0441\u044f, \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u044f\u0437\u044b\u043a\u0430 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u044d\u0442\u0438\u043c \u0438 \u0443\u044f\u0441\u043d\u0438\u043b, \u0447\u0442\u043e \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f. \u0421\u0442\u0440\u0430\u043d\u043d\u043e, \u043d\u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043e\u0434\u043d\u043e\u0433\u0440\u0443\u043f\u043f\u043d\u0438\u043a\u0430 \u043d\u0435 \u0432\u044b\u0434\u0430\u043b\u2026<\/p>\n<p>  \u0413\u0434\u0435-\u0442\u043e \u0447\u0438\u0442\u0430\u043b, \u0447\u0442\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442 C++14, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0442\u0440\u044e\u043a. \u041d\u043e \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0433\u043e\u0432\u043e\u0440\u0438\u0442, \u0447\u0442\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c constant expression. \u042d\u0442\u043e \u0435\u0449\u0435 \u0440\u0430\u0437 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441 C99, \u0433\u0434\u0435 VLAs \u0434\u0430\u0432\u043d\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-195148","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/195148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=195148"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/195148\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}