Differences between [] Static arrays and dynamic arrays allocated by new in C ++, array new
This article mainly introduces the differences between [] Static arrays and dynamic arrays allocated by new in C ++. It is an important concept. For more information, see
This article uses examples to analyze the differences between [] Static arrays and dynamic arrays allocated by new in C ++, which can help you better understand arrays in C ++. The specific differences are as follows:
1. When performing sizeof operations on static array names, the result is the space occupied by the entire array;
Therefore, you can use sizeof (array name)/sizeof (* array name) to obtain the length of the array.
Int a [5]; then sizeof (a) = 20, sizeof (* a) = 4. Because the entire array occupies 20 bytes, the first element (int type) occupies 4 bytes.
Int * a = new int [4]; then sizeof (a) = sizeof (* a) = 4, because the number of addresses is 4 bytes, the int type also occupies 4 bytes.
2. When a static array is used as a function parameter, sizeof is performed on the array name in the function. The result is 4 becauseThe pointer represented by the array name is an address.It occupies 4 bytes of memory (because the compiler does not check the length of the array when passing the parameter of the array name, for details, refer to the previous article c ++ on array reference instance analysis ). For the function name of the dynamic array, the sizeof operation is performed at any time, and the result is 4.
3. delete is also required for new. It allocates space in the heap, which is less efficient. [] allocating space directly on the stack will be automatically released, which is highly efficient, but the stack space is limited.
4. The problem of returning an array through a function
The static array declared by the function cannot be returned through the function. Due to the lifetime problem, the memory occupied by the internal variables of the function is released after the function is called. If you want to return an array through the function, you can use new to dynamically create the array, and then return its first address.
The reason is as follows: [] Static arrays are applied in the stack, while local variables in the function are also in the stack, while new dynamic arrays are allocated in the heap, therefore, after the function is returned, the items in the stack are automatically released, and the items in the heap are not automatically released without the delete operation.
Example:
Int * test (int * B) // B can be the array name of the static array or the first address of the Dynamic Array {for (int I = 0; I <5; I ++) // outputs the cout elements of the input array <* (B + I) <""; cout <endl; int * c = new int [5]; // dynamically create an array // If the green part is converted to int c [5]; the main function calls test and cannot obtain the c array for (I = 0; I <5; I ++) // The values of the new array are equal to the values of the input array plus 5 * (c + I) = * (B + I) + 5; return c; // return the first address of the newly created dynamic array} int main () {int * B = new int [5]; // create a dynamic array B for (int I = 0; I <5; I ++) // value * (B + I) = I; // the green part can also be converted to int B [5] =, 4}; that is, the static array int * c = test (B); // uses B as the parameter, calls the test function, and returns the value to c for (I = 0; I <5; I ++) // outputs the cout of the array returned by test <* (c + I) <"; cout <endl; return 0 ;} /* hovertree.com */
I believe that after reading the examples in this article, you can further deepen your understanding of the C ++ array.
Recommended:
Http://www.cnblogs.com/roucheng/p/cppjy.html