C + + Array initialization method

Source: Internet
Author: User
Tags array definition

Defined:

int New int [//  array of ten uninitialized ints

This new expression assigns an array containing 10 elements of type int and returns a pointer to the first element of the array, which initializes the pointer Pia.

An array object created in a free store has no name and can only access objects in the heap indirectly through its address.

Note: C + + uses new and delete to allocate and release dynamic arrays on the heap (free storage area).

Dynamic Array Initialization:

1. An element can only be initialized to the default value of an element type, not as an array variable, providing a different initial value for an array element with an initialization list.

2. For arrays of built-in data type elements, you must use () to display the specified program to perform the initialization operation, or the program does not perform an initialization operation:

int New int [//  Each element is not initialized with intnewint[ten] ();  // each element is initialized to 0

3. An array of class-type elements, whether or not used (), automatically calls its default constructor to initialize:

string New string [ten];  // each element invokes the default constructor initialization string New string [Ten] ();  // each element invokes the default constructor initialization

To dynamically allocate an empty array:

Char New Char [0];

After that, you can dynamically change the dimensions of the CP.

Dynamic release:

Delete [] PIA;

Typical use examples:

Const Char*PC ="a very long literal string";//using the const pointer when working with C-style stringsConstsize_t len = strlen (PC) +1;//size_t for the size and subscript of an array for(size_t IX =0; IX! =1000000; ++ix) {Char*PC2 =New Char[Len];//The contents of the storage space that PC2 points to will change dynamically, so do not use the conststrncpy (PC2, PC, Len);//using strncpy is safer than using strcpy//Do something;    Delete[] PC2;}

Reference: C + + Primer Learning notes: Dynamic arrays

First, array definition and initialization
   1: one-dimensional array initialization:2: Standard Way One:intvalue[ -];//Value[i] With variable value, no initialization   3: Standard way Two:intvalue[ -] = {1,2};//value[0] and value[1] values of 1 and 2, respectively, and no defined value[i>1]   4://is initialized to 0   5: pointer mode:int* Value =New int[n];//not initialized   6:Delete[]value;//must not forget to delete the array space   7:     8: Two-dimensional array initialization:9: Standard Way One:intvalue[9][9];//value[i][j] With variable value, no initialization  Ten: Standard way Two:intvalue[9][9] = {{1,1},{2}};//value[0][0,1] and value[1][0] values are initialized, others are initialized to 0   One: pointer mode one:int(*value) [N] =New int[M][n];  A:Delete[]value;//n must be a constant, call intuitive. Not initialized   -: pointer mode two:int* * value =New int*[M];  -: for(i) value[i] =New int[n];  the: for(i)Delete[]value[i];  -:Delete[]value;//multiple destruction, storage hassle, uninitialized   -: pointer mode three:int* Value =New int[3][4];//The storage of an array is stored by row   -:Delete[]value;//Be sure to release the memory, otherwise it will cause a memory leak   +:     -: Multidimensional array initialization: +: pointer mode:int* Value =New int[M] [3][4];//only the first dimension can be a variable, and the other dimensions must be constants, or they will error   A:Delete[]value;//Be sure to release the memory, otherwise it will cause a memory leak

Add ";" After the curly brace of the array initialization. to indicate the end.

Array Access:

Pointer form: Accessed as a two-dimensional array value[i][j]:

* (Value[i] + j) or

(* (value + i)) [j]

Second, the array is passed as a parameter
   1: one-dimensional array parameter passing:2:voidFunc (int*value); 3: Or is4:voidFunc (intvalue[]); 5:     6: Two-dimensional array delivery:7: Definition isint**the delivery of value;8:voidFunc (int**value); 9: Definition isint(*value) [N] =New int[m][n]; the transferTen:voidFuncint(*value) [n]);//sizeof (P) =4,sizeof (*value) =sizeof (int) *n;
Three, array and pointer relationship

1, the connotation of the array name is that the reference entity is a data structure, this data structure is an array;

2, the extension of the array name is that it can be converted to a pointer to its reference entity, but also a pointer constant;

3, pointer to the array is another type of variable, (under the Win32 platform, the length of 4), only means that the group storage address.

4, the array name as a function parameter, in the function body, it loses its own connotation, just a pointer, and in its loss of its connotation at the same time, it also lost its constant characteristics, can be self-increment, self-reduction and other operations, can be modified.

Iv. storage format for arrays

Multidimensional arrays are stored in memory in the lowest-dimensional contiguous format, such as the two-dimensional array {{1,2},{3,4}} in memory where the "1,3,2,4" is the order, which is different from Matlab,matlab is stored by column . Useful when using pointers for indexing.

Five, character array

An array of type char is called a character array, which is typically used to store strings. A string is a sequence of characters appended with a special character (a trailing flag). The string termination character indicates that the character has ended, defined by the escape sequence '% ', which is sometimes referred to as a null character and occupies one byte, where 8 bits are all 0. This form of string is often referred to as a type C string, because defining a string in such a way is introduced in the C language, using string in C + +, and the CString class is defined in MFC.

Each character in a string occupies one byte, and the last null character is counted, and the string requires more bytes than the number of bytes contained. Such as:

Char movie_star[15] = "Marilyn Monroe";

Here the string is 14 characters, but to define an array of 15 strings. You can also not specify the number of character arrays. Such as:

Char movie_star[] = "Marilyn Monroe";

VI. Memory leaks

We define a pointer, give it an address value, and then no longer use it, but without delete, the original memory will not be freed when the pointer is given a different address value, which is called a memory leak.

Reference: [C + +] array initialization

C + + Array initialization method

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.