New and new [] Operators

Source: Internet
Author: User

Functions with the same name under the same scope can be reloaded as long as the parameter list is different. Reload new and new [
] Operator, the 1st parameters of the function are fixed, indicating the length of the memory to be allocated. You can add parameters without changing the 1st parameters, and reload multiple new and new [
] Operator functions. A more practical example is the new and new [] operators for the reload locating, that is, adding a pointer type parameter to the new and new [] overload functions, new and new [
] When the expression is called, the memory address at the specified position of the pointer is returned.

1 // program example

2
# Include "malloc. h"

3
# Include <iostream>

4
Using std: cout;

5
Using std: endl;

6

7
Class Point

8
{

9
Public:

10
Double x, y;

11
Point (double x1, double y1): x (x1), y (y1)

12
{

13
Printf ("1 Point object created/n ");

14
}

15
~ Point ()

16
{

17
X = y = 0;

18
Printf ("1 Point object released/n ");

19
}

20
Void * operator new (size_t, double * p)

21
{

22
Return p;

23
}

24
Void operator delete (void *, double *)

25
{}

26
};

27

28
Void print (double * d, int n)

29
{

30
For (int I = 0; I <n; ++ I)

31
Cout <d [I] <"";

32
Cout <endl;

33
}

34
Int main ()

35
{

36
Double d [6] = {1.1, 1.2, 1.3, 1.4, 1.5, 1.6 };

37
Print (d, 6 );

38

39
Point * p = new (d + 2) Point (100,100 );

40
Print (d, 6 );

41

42
P-> Point ::~ Point ();

43
Print (d, 6 );

44

45
Return 0;

46
}

Display result:

1.1
1.2 1.3 1.4
1.5 1.6

One Point object is created

1.1
1.2 100 100
1.5 1.6

One Point object is released

1.1
1.2 0 0
1.5 1.6

 

 

 

When the new operator is located to perform secondary allocation on the allocated memory, to avoid the same memory being released multiple times, it is best not to use the delete expression to release the memory allocated by the new operator. In this example, the memory of array d comes from the stack, and the main function is automatically released when it ends.
Expressions such as p will inevitably cause program errors. So, in order to call the Point Object destructor without using the delete expression, we can display and call the Destructor like the code in line 42nd.

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.