Variable-length array-Go

Source: Internet
Author: User
Tags function prototype

http://ericwang.github.io/program/2010/02/10/c_Variable_length_arrays/

Variable length arrays (variable-length array) in C

Variable length arrays is an attribute of C99, not c++98, a variable-length array of C99 standards, which is described in the standard 6.7.5.2 array declarators:
2.Only Ordinary ID Entifiers (as defined in 6.2.3) with both block scope or function prototype scope and no linkage shall has a variably mod ified type. If an identifier are declared to being an object with the static storage duration, it shall not has a variable length array type. In other words, a variable-length array cannot be in a static store (including global variables and static variables).
In addition, VLA needs to support sizeof operations, and dynamic sizeof is a unique feature of C99.
currently many C + + compilers do not yet support dynamic array features (vc++2005 does not support this feature, GCC3.2 is supported later, the previous version is not investigated, and it is not known from which version to support this feature). GCC documentation says: Variable-length automatic arrays is allowed in iso C99, and as an extension GCC accepts them in C89 mode and in C + +. So, with GCC, you can still use dynamic arrays even if you open the-ansi (equivalent to-std=c89) option.
actually C99 's VLA is not really a variable-length array-it simply generates an array at run time based on a variable, and the array does not change after that, and the real-meaning variable-length array can be dynamically scaled when actually used.
in the past C and now C + +, the offset of the automatic variable is predetermined, but VLA cannot do so.
In the following example, because B, the C size is indeterminate, their address must be determined at run time

void test(int tmp){int a;scanf("%d", &a);int b[tmp];int c[a];printf ("size(b) = %d, size(c) = %d\n", sizeof(b)/sizeof(int), sizeof(c)/sizeof(int));}

VLA allocates space on the stack, which is fast. Let's give an example of a typical 32-bit compiler under a function implementation. In this implementation, the following two registers focus on the function stack:

EBP: function occupies the position of the beginning of the content frame, esp: the current stack top

In general, the number and size of automatic variables in a function are deterministic. For example, there are 4 int, a float array of size 10. The compiler can simply set aside all the space in ESP-= 56 (for example, a 32-bit PC). The offset of each variable relative to EBP is also deterministic, so if he accesses int a,

$a = 4, he would visit: $a [EBP]

For VLA , things are a little more complicated. Now there is a VLA c. His size is uncertain-so its offset is also uncertain: if it had another VLA B before it could not know where it was. The current practice in GCC is that, in the case of a variable array, it is directly accessed by the Register + offset, and in many cases, the starting address of the array is first obtained from the stack, and then the elements of the array are accessed in an offset manner.

This operation is very fast. However, one problem is that the stack size is fixed and not very large. You can't allocate a lot of stuff. The array is also temporary-once you exit the current scope, he can be brushed off by any other function.

A review of the characteristics of variable-length arrays (excerpt from 2):

1. Variable-length arrays are allocated on the stack, in fact, from the perspective of semantics should be, variable-length array or an array, or a local variable, in C language, local variables are allocated on the stack, malloc is allocated on the heap. There is no memory leak in this, because the memory on the stack does not need to be managed by the programmer

2. When the variable-length array and other variables are in a single scope, the variable-length array is allocated last, that is, at the bottom of the stack (the lowest address). This is also the case of variable-length arrays different from ordinary variables, the memory allocation of ordinary variables is defined in order to define the first allocation, but the variable-length array is not so, the variable-length array in a scope is put to the last allocation, the reason is very simple, the size of the variable-length array can not be determined at compile time, Therefore, if the variable-length array is allocated after the space of ordinary variables, then the access to the subsequent ordinary variable is not convenient, of course, the simple situation can be solved by optimization, but if there are a lot of variable arrays and ordinary variables mixed together, the optimization is very difficult to do well, so the variable-length array is placed in the final allocation, is a more reasonable approach;
Of course, it is not necessary to put in the final, according to the normal definition of the allocation of space is also possible, but in the access variable array of variables, you need to first find the variable array start address, and then down (low address) to find the address of the following variable, access to the corresponding variable. The current use of GCC in the final approach, can also be understood as an optimization. 。

3. For element access to the variable-length array, the first address can be placed in the register, the first address can be placed in the stack, and then the element is accessed by the first address, and then the element is accessed by an offset.

4. Variable-length arrays cannot be in static storage, it is obvious that the static store determines the size of the allocated memory at compile time, not as dynamic as the stack, and the variable-length array is clearly not defined in the static store.

5. Change the meaning of the array, I think there is some meaning in C, if it is in the performance, memory requirements are very strict, it is convenient to allow the declaration of a variable length array, because the original need a piece of contiguous memory of the place or declare a larger, sure enough, but so wasted, In the memory requirements of the place is not good, another approach is malloc dynamic allocation, but the disadvantage is the need to manually manage, to manually free, the program if large memory management is not good words easy memory leaks, so in C still have some meaning. But in C + + There are already vectors, and C + + is not much used in the performance, memory requirements very high place,

6. Using variable-length arrays, the most important of course is to check the validity of the size of the array, I have an example in order to reduce the interference factor, did not do a check, the actual program must do this check, then the interview when such a program must be kick immediately. In addition, there is another way to stack overflow, the array size if it is negative, you can jump to the wrong place to jump.

Variable-length array-Go

Related Article

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.