Use of new and delete in C + +

Source: Internet
Author: User

1. Creating a dynamic array

There are three important restrictions on array-type variables: The length of the array is fixed, and it must be known at compile time, and the array exists only in the block statement that defines it. For dynamically allocated arrays, although the length is fixed, the dynamically allocated array does not have to know its length at compile time, and the array length can (and usually is) be determined at run time, and the dynamically allocated array persists until the program shows its release so that we can decide for ourselves whether the array exists or not.

Each program consumes a piece of available memory space at execution time, which is used to hold dynamically allocated objects, which are called free stores of programs or heaps (heap). The C + + language uses new and delete to allocate storage space in the free storage area.

When allocating an array dynamically, you only need to specify the type and array length, not the array object, and the new expression returns a pointer to the first element of the newly allocated array:

int *pia=new int[10];

This new expression assigns an array with 10 elements of type int and returns a pointer to the first element of the array. Array objects created in free storage are not named, and programmers can access objects in the heap indirectly through their addresses.

When an array is dynamically allocated, if the array element has a class type, initialization is implemented using the class's default constructor, and no initialization if the array element is a built-in type:

String *PSA = new STRING[10]; Invokes the default constructor of the string class to initialize each element of the array in sequence.

int *pis = new INT[10]; No initialization value

A pair of empty parentheses following the length of the array can be used to initialize the values of the SET elements:

int *pis = new INT[10] (); Array elements are set to 0

The reason that arrays are dynamically allocated is often due to the fact that the length of the array is not known at compile time. Allow for dynamic allocation of empty arrays in C + +:

size_t n = get_size ();

int *p = new Int[n];

for (int *q=p;q!=p+n;++q)

/* Handle the related code of the array element */"

In the example above, the value of n can be determined only when the program is running. If the value of n is 0, the code is still executed correctly. Although the definition of an array variable of length 0 is not allowed in C + +, it is clear that calling new dynamically creates an array of length 0 is legal.

Char *CP = new CHAR[10];

When you dynamically create an array of length 0 with new, new returns a valid non-0 pointer, but cannot be dereferenced because it does not point to any elements.

2. Create a single object dynamically

When you create an object dynamically, you only need to specify its data type, you do not have to name the object, and the new expression returns a pointer to the newly created object, which we access through this pointer:

int *pi = new int (1024);

String *ps = new String (10, ' 9 ');

Using the direct initialization syntax rules in C + + to initialize dynamically created objects, the above expression initializes the dynamically created object using that syntax rule. For objects of class type, the object is initialized with the constructor of the class type. You can initialize values for dynamically created objects as follows:

String *ps = new string ();

int *pi = new int ();

For class types that provide a default constructor, it is not necessary to initialize the values. If the initialized dynamically created object is not displayed, the object of the class type is initialized with the default constructor of the class, and for objects of the built-in type, there is no initialization.

3. Release of dynamic space

The dynamically allocated memory must eventually be freed. If you do not need an array that is dynamically created, we must display the storage space it occupies to the free store of the program. Use the delete [] expression in C + + to release the array space that the pointer points to:

delete [] PiS;

This statement frees the storage space occupied by the dynamic int array created above. Between the keyword delete and the pointer [] tells the compiler that the pointer points to an array in the free store, not a single object. For an expression

Delete pi;

This command frees the memory space occupied by the Int object that the pi points to. When the pointer is removed, the pointer changes to a dangling pointer (dangling pointer). The dangling pointer points to the memory of the object that was previously stored but the object no longer exists. Dangling pointers often cause program errors and are difficult to detect. Once you have deleted the object pointed to by the pointer, set the pointer to 0 immediately, which makes it very clear that the pointer is no longer pointing to any object.

Transferred from: http://blog.csdn.net/hippig/article/details/8239573

Use of new and delete in C + +

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.