New and delete Operators

Source: Internet
Author: User
ArticleDirectory
    • I. Change of PF rate when the new and delete operators are used
    • 2. pointer to the continuous space
    • Iii. Notes for delete/Delete []

In C, you need to use free to release the memory for applying for the test memory space using malloc and calloc. In C ++, the space applied for by the new function must be released using Delete.

I. Change of PF rate when the new and delete operators are used

CTRL + ALT + DEL enter the task manager, performance, and run the followingCodeAnd observe the change of PF rate. We can see that the new operator increases the PF rate and the delete operator restores the PF rate. Note: to use the new space, delete must be used to release the space. To use the new [] space, delete [] must be used to release the space. They cannot be used together. After a continuous space is allocated with new [], the pointer variable "points to" the first address of the space.

# Include <iostream. h>
# Include <stdio. h>

Int main (INT argc, char * argv [])
{
Cout <"start to allocate memory by pressing any key" <Endl;
Getchar ();

Unsigned char * P = new unsigned char [1024*1024*100];
Cout <"MB of memory allocated successfully" <Endl;
Getchar ();

Delete [] P;
Cout <"Release allocated M memory" <Endl;
Return 0;
}

2. pointer to the continuous space

After pointing to a continuous space through new [], p becomes very similar to a one-dimensional array. Let's first review the knowledge about arrays. Suppose this is an array: int arr [20];

Compared with pointer variables, an array does not have a separate memory space and stores its memory address. That is, the pointer Variable P is an independent variable, but its value points to another contiguous memory space. The array arr itself represents a continuous space.
The array is a real address and cannot be changed. When you define an array, you must obtain an address based on its location in the memory, for example, "0x1a000000 ". If this array exists, its lifetime address is the value.
The pointer is a "virtual" address, which can change the value of the address. When you define a pointer variable that occupies 4 bytes of memory, you can write any value to the 4 bytes of memory, which is considered as a memory address. For example, you can write the above "0x1a000000". At this time, the pointer P points to the first element. You can also change it to "0x1a000003". At this time, the pointer P points to the second element.
Therefore, when P points to a continuous space through new [], p is a pointer to an array, and * P is an array.

Similarities:
Array:

Int arr [20]; // Definition
Arr [0] = 100; // set the first element to 100
For (INT I = 1; I <20; I ++)
{
Arr [I] = arr [I-1] + 50;
}
For (INT I = 0; I <20; I ++) // output
{
Cout <arr [I] <Endl;
}
// Get the specified Element Through +, or use []
Cout <* (ARR + 0) <Endl; // * (ARR + 0) equals to * arr
Cout <* (ARR + 1) <Endl;
Cout <* (ARR + 1) <Endl;
Pointer:

Int * P = new int [20]; // Definition
P [0] = 100; // set the first element to 100
For (INT I = 1; I <20; I ++)
{
P [I] = P [I-1] + 50;
}
For (INT I = 0; I <20; I ++) // output
{
Cout <p [I] <Endl;
}
// Get the specified Element Through +, or use []
Cout <* (p + 0) <Endl; // * (p + 0) equals to * P
Cout <* (p + 1) <Endl;
Cout <* (p + 1) <Endl;
Differences between the two:
Array:

// Define and initialize
Int arr [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// You cannot change the position of an array by adding + or-to the array itself.
Arr = arr + 1; // error!
Cout <* arr <Endl;
Arr ++; // error!
Cout <* arr <Endl;
Arr --; // error!
Cout <* arr <Endl;

// The Space contained in the array is automatically allocated and recycled by the system.ProgramTo release directly
Pointer:

// Define and generate a space, but cannot directly initialize the content of the space
Int * P = new int [20] {0, 1, 2, 3, 4 ......}; // Error!

// You have to set them one by one through a loop
For (INT I = 0; I <20; I ++)
{
P [I] = I;
}

// You can directly change the pointer through the + or-operation.
P = p + 1;
Cout <* P <Endl;

P ++;
Cout <* P <Endl;

P --;
Cout <* P <Endl;

// Pointer to the continuous space, which must be released using Delete []
Delete [] P;

Iii. Notes for delete/Delete []

1. the pointer uses new or new [] to "Apply" to the system to obtain a memory space, which must be released without any need.

Int * P = new int [100];
Int girl [100];

P = girl;
Delete [] P;
A disaster occurs when Delete [] p. The original intention is to release the memory space P initially obtained through new int [100], but in fact, P has already pointed to girl [100. Result 1: The initial space is not released. Second, the girl [100] space is allocated to heap, which is released by the system itself, but we have to force release it.

2. When a pointer is deleted, it should point to the original address
When a pointer changes its direction through the +,-and other operations, ensure that it returns to the original direction before release. As shown in the following figure: When Delete [] P, P points to the second element. As a result, the release produces a dislocation: the first element is not released, one more element is deleted at the end.
Int * P = new int [3];

* P = 1;
Cout <* P <Endl;

P ++; // The point of P changes, pointing to the next element.

* P = 2;
Cout <* P <Endl;

Delete [] P; // how to eliminate this serious error when releasing an error?
The first method is to correctly "Inverted" the pointer back to the original position:
P --;

Delete [] P;
However, when our Pointer Points to a change many times, it will be difficult to ensure one-to-one return before release. Therefore, the other method is to "Back up" a copy at the beginning. Release the pointer directly when releasing the pointer.

Int * P = new int [3];

Int * pbak = * P; // backup

// Move P

......

Delete [] pbak; // release because pbak points to the address originally allocated by P, we delete pbak, which is the initial point of deleting p. In this case, we cannot delete p again. This raises the last question in this chapter about new/delete and new []/Delete.

3. Released space cannot be released again

The first is the most direct:
Int * P = new int (71 );

Cout <* P <Endl;

Delete P; // OK!

Delete P; // error! Delete P again

The second is to repeatedly Delete multiple pointers to the same point.

Int * P1 = new int (71 );

Int * P2 = p1; // P2 and P1 now point to the same memory address

Cout <* P1 <Endl;

Cout <* P2 <Endl;

Delete P1; // OK

Delete P2; // error! The memory indicated by P2 has been released through Delete P1 and cannot be deleted again. If P2 is deleted first, it cannot be deleted again.

Delete P2; // OK

Delete P1; // Error

The third is to delete the pointer to a common variable.

Int A = 100;

Int * P = &;

Delete P; // error P does not get new memory space, but directly points to fixed variable. Therefore, deleting P is equivalent to forcing the inherent space of A, which may lead to errors.

From: http://blog.csdn.net/slayers_nada/archive/2009/05/29/4222267.aspx

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.