static arrays and dynamic arrays

Source: Internet
Author: User

Concept

Arrays are widely used in program design and can be used to store large amounts of the same type of data in different types of arrays. There are generally three ways to create an array:

An array of global/static ranges, an array of local variables, and an array of request heap space creation. Where both the global/static range array and the local variable array are static arrays, the arrays that are created from the request space in the heap are dynamic arrays.

The difference between a static array and a dynamic array

1, the size of the static array is determined during compilation, and allocated, its memory at the end of use by the computer automatically released, high efficiency; dynamic arrays are used by programmers to dynamically request from the heap memory when the program is running, and are released by the programmer after the end of the application, which is inefficient.

2. When the sizeof operation is performed on a static array, the result is the size of the entire array, and the result is a constant of 4 when the sizeof operation is performed on the dynamic array.

So you can get the length of the array with sizeof (array name)/sizeof (* array name) or sizeof (array name)/sizeof (element type).

int a[5]; Then sizeof (a) =20,sizeof (*a) =4. Because the entire array occupies 20 bytes, the first element (int) is 4 bytes.
int *a=new int[4]; sizeof (a) =sizeof (*a) = 4, because the number of address bits is 4 bytes, and the int type also accounts for 4 bytes.

3, static array as a function parameter, in the function of the array name sizeof operation, the result is 4. Because the array name is an array pointer, which is an address that occupies 4 bytes of memory (because the compiler does not check the length of the array when it passes the parameters of the arrays name.) For the function name of the dynamic array, the result is 4 whenever the sizeof operation is performed.

4, a static array declared within a function is not possible to return through the function, because of the problem of the lifetime, the function calls the memory of its internal variables is freed. If you want to return an array from a function, you can dynamically create the array with new in the function and return to its first address. The static array is applied in the stack, and the local variables in the function are in the stack, and the new dynamic array is allocated in the heap, so when the function returns, the requested memory in the stack is automatically freed, and the memory requested in the heap is not automatically freed without the delete.

A program to illustrate the difference between a static array and a dynamic array

//first declare several arrays:intg_a[Ten];//array of global variablesintMainvoid){    inta[Ten];//array of local variables    Static ints_a[Ten];//static local variable array    int*p1_a, *p2_a;//Array Pointers//request space for a dynamic arrayP1_a = (int*)malloc(sizeof(int) *Ten); P2_a=New int[Ten]; //assigning values to arraysa[7] =0; s_a[7] =0; g_a[7] =0; p1_a[7] =0; p2_a[7] =0; //free up space and position the pointer 0    Delete[] p2_a;  Free(p1_a); P1_a= P2_a =0;}

In the above program, the 5 arrays are almost identical to each other except for the variable names when they are assigned, is their implementation the same?
The answer is no, dynamic arrays and static arrays, although they do not seem to make much difference when they are used, are not the same.
Disassemble and look at their code.

Array type C + + code Assembly implementation Brief description
Local variables A[7] = 0; MOV DWORD PTR ss:[ebp-c], 0 Using EBP to position variables on the stack
[EBP-28] a[0]
...
[EBP-4] a[9]
Static local Variables S_A[7] = 0; MOV DWORD PTR ds:[4c5e5c], 0 Static variables are placed in the Data section.
Global variables G_A[7] = 0; MOV DWORD PTR ds:[4c5e84], 0 Global variables, like static variables,
will be placed in the Data section.
array pointer
p1_a[7] = 0; mov eax, DWORD PTR ss:[ebp-2c]
MOV DWORD PTR ds:[eax+1c], 0
0X1C/4 = 7
Array pointers
(new)
P2_A[7] = 0; MOV EAX, DWORD PTR ss:[EBP-30]
MOV DWORD PTR ds:[eax+1c], 0
Ditto

From an addressing standpoint, a static array is addressed directly, whereas a dynamic array is two-time addressing, which is related to the implementation of the dynamic array itself. The variable of a static array is itself the address of the first element of the array. A variable of a dynamic array holds a first-address pointer to the application space.
Even for a static two-dimensional array or a multidimensional array, the truth is composed of an array of multiple address contiguous arrays.

For example: int a[35] and int b[7][5] for the former, which is an array of 35 int type values and for the latter, it can be understood that B is an array with 7 array types, and each array type is a An array of 5 values of type int. The result is that the distribution of two arrays in memory is actually the same.

How dynamic arrays are created:

Type (*P) [N] = new TYPE [][n];

Where type is a kind, n is the number of columns in a two-dimensional array. In this format, the number of columns must be indicated, while the number of rows does not need to be specified. Here, the type of P is type*[n], which is a pointer to an array of N-column elements.

There is also a way to not specify the number of columns in an array:

int **p;
p = new INT*[10]; Note that int*[10] represents an array of pointers with 10 elements
for (int i = 0; I! = ten; ++i)
{
P[i] = new INT[5];
}

Here is a pointer to the pointer, which points to a pointer array containing 10 elements, and each element points to an array of 5 elements, thus constructing an array of 10 rows and 5 columns. The address of each row element is contiguous, but the address between the row and the row is discontinuous. For example p[0][9] and p[1][0] The address of two elements is discontinuous.

When the array is used, the code that frees the space is:

for (int i = 0; I! = 5; i++)
{
Delete[] p[i];
}
Delete[] p;

Arrays and pointers

int A[10];int *pa = A;

After declaring these two variables, we can:
A[0] = 0;
Again we can:
*a = 0; One-time addressing
*PA = 0; Two-time addressing
Pa[0] = 0; Two-time addressing

For *a and *PA, the prototype is actually:
* (A + 0)
* (PA + 0)

dynamic Arrays (array pointers)

The dynamic array has a local--its pointer,
But once new is over, the actual allocated space it points to is inside the heap.

There are two ways to request space in the heap: malloc for C and new for C + +.
For arrays of basic data types, there is no big difference between the two ways of applying space.

int *pa, *PB;PA = (int *) malloc (sizeof (int) * 10); Correct PB = new int[10]; That's right

However, if you have an array of classes, you must use new to allocate space, not malloc.

MyClass *pa, *pb;pa = (MyClass *) malloc (sizeof (MyClass) * 10); Error PB = new MYCLASS[10]; That's right

With malloc, only a space with a size of sizeof (MyClass) * 10 is allocated, and nothing else is done.
With the new call, not only is space allocated (automatic calculation), but each MyClass constructor is also called.
For classes, constructors are important, and unexpected results can occur if you use a class variable without calling the constructor.
Again, after applying for space with new [], you need to use Delete [] to free up space.
Why not delete, but delete []?
For arrays of basic data types,delete frees only pa[0] space, and delete [] correctly frees all space.
For An array of classes,delete only calls the destructor of pa[0], and it is space-
Delete [] invokes the destructor of all elements and frees all space correctly.

Because there is a fact:
After adding a value to the pointer, it is not simply adding the value to the address.
Instead, an offset is represented, and the offset unit amount is different depending on the type.

static arrays and dynamic arrays

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.