Memory huge (below)

zhaozj2021-02-12  167

Memory huge (below)

Foreword: It may be that the number of articles is too long, I don't know why I can't publish together, so it is divided into two parts, which is the second part. Please readers are patient :)

3. How to freely call the overloaded Operator New and the system Operator New?

#include

#include

#include

#include

#include

Using std :: cout;

Using std :: end1;

Using std :: vector;

Class Memory

{

PUBLIC:

Memory () {}

~ Memory () {delete PTR;

Void * PTR;

Std :: size_t size;

Std :: size_t line;

}

Vector v;

Void * Operator new (std :: size_t size, std :: size_t line = __ line__)

Throw (std :: bad_alloc)

{

Memory PM;

COUT << "<< size << endl;

PM.ptr = std :: operator new (size); // I hope to call the system's Operator New, how to use it?

Pm.size = size;

PM.Line = line;

v.push_back (pm);

Return PM.PTR;

}

Void Operator Delete (Void * PTR)

{

IF (PTR == 0) Return;

Std :: Operator Delete (PTR);

}

int main ()

{

INT * i = new int (3); // always call the Operator New I wrote!

Return 0;

}

Replace Operator New, Operator Delete, Array New, or Array Delete Replace the Global version to custom version, which is almost not a good idea - even if C standards allow you to do this. The standard versions of these functions are generally optimized for the storage management of General-purpose, and user-defined alternatives don't make a good meeting. (However, for a specific category or class class system, the operation of the "custom) member function is customized to customize its memory management, it is usually reasonable.) [Note 3]

Note 3: Please refer to "C Gotcha" ITEM62: Replacing Gotcha "written by Stephen C. Dewhurst.

This program has a lot of problems, I will come one by one.

(1) Operator should be all in the global name space (can be used as a member function when customized), that is, , -, *, / ... are all in Global Namespace, Operator New is also a Operator. So std :: operator new (size); this usage is wrong, you should change :: Operator new (size);

(2) void * Operator new (std :: size_t size, std :: size_t line = __ line__) throw (Bad_alloc) There are two Operator New in Global Namespace (one written by one, built-in one) in this function In this, there is such a sentence, pm.ptr = :: Operator new (size); Do you say that this Operator new is the one calling built, or recursively call itself? You don't know, I don't know, the compiler doesn't know, so there is ambiguity when compiling! The solution is to let the second parameter of the Operator New, do not have the default quotes (argument). Operator delete is called directly here. (3) The design of the Memory class has problems, and the destructive function calls Delete,

{

Memory PM; // Timed bomb starts

...

Return PM.PTR;

}

// Call Memory :: ~ Memory (), bomb explosion, accompanied by the devil's laughter.

The solution is to rewrite the destructor of Memory is empty!

The last program is as follows:

// No change

Class Memory

{

PUBLIC:

Memory () {}

~ Memory () {} // revised

Void * PTR;

Std :: size_t size;

Std :: size_t line;

}

Vector v;

Void * Operator new (std :: size_t size, std :: size_t line) throw

(std :: bad_alloc) // Revised

{

Memory PM;

COUT << "<< size << endl;

PM.PTR = :: Operator new (size); // Call the system's Operator New

Pm.size = size;

PM.Line = line;

v.push_back (pm);

Return PM.PTR;

}

Void Operator Delete (void * p, std :: size_t)

// Match overloaded Operator New

// Attention, when we overload Operator Delete, these overload versions will never be evoked by "Express in the form of standard delete".

// In this example, you can do not overload Operator Delete, and the built-in is already enough! Behind you can use Delete I directly; don't need ugly Operator Delete (i, 1);

{

:: Operator delete (p); // Has been processed P = 0

}

int main ()

{

INT * i = new (__ line__) int (3); // call me written Operator New, Evised!

// delete i; // This form will call Global Operator Delete, of course, it is complete!

// i-> ~ int (); // is not legal, but if there is a custom type with the NontriVial Destructor, the display call Destructor is inevitable!

Operator delete (i, 1); // call yourself overloaded Operator Delete

Return 0;

}

How ugly, do you really need to customize Operator New and Operator delete? Please think twice!!

4. Take a look at this output: cout << (cout << "HelloWorld" << Endl) << endl; answer: << is a left shift operator (SHIFT OPERATOR), C overloads it, becomes output Different operators, and C has been overloaded for various basic types, so we can naturally use the << Output Int, Float, Double, Char *, ..., remember Operator << for Void *, also (It will be important later!), So if you want to use << to output a custom type, you must overload Operator <<. Let's take two steps to process the above problems;

(1) First look (cout << "HelloWorld" << Endl), output helloworld, wrap, refresh buffer. Here you keep in mind that the return type of Operator << is std :: ostream &; std :: cout is an object of the type std :: Ostream.

(2) std :: ostream is a TypedEf. Typedef Basic_OStream > ostream; Basic_OStream parent class is Basic_ios; Basic_ios has a MEMBER FUNCTION: Operator Void * (), transition operator. It is already very clear now! (COUT << "HelloWorld" << Endl) returns itself: cout << (cout) << Endl; (cout) calls Operator void * () implicit transformation into a pointer. Then, the pointer to the type of Void * is then output, wrap, and refresh buffer. (The use of Operator Void * () is mainly to determine whether the status of the Stream failed, used in a continuous input in the form of while (cout << * p)). As for the final pointer value, it is related to the implementation!

5. Const A & a :: Operator = (const A & Other)

{

IF (this! = & taher)

{

THIS-> ~ A ();

NEW (THIS) A (Other);

}

Return * this;

} Is there a mistake?

Answer: This question is made once again confirmed a fact, a sentence is not what is content, is it meaningful, but who said this sentence is! HUSTLI said this sentence, someone will say that he should look at the software engineering, don't show off the skills; but if it is Herb Sutter, there will be no one will want him to make up the software engineering class, most people Will be serious to see what he proposed. (The fact is that HERB SUTTER has indeed proposed a similar problem, and it is very beautiful with PIMPL techniques!)

(1) Const A & should be A &, Operator = should return a left value that can be changed, because we can use int A, b = 2, c = 4; (a = b) = C; so we should also like this Write: a a, b, c; (a = b) = C; isn't it? So you can't return Const Reference.

(2) IF (this! = & Other) {

THIS-> ~ A ();

NEW (THIS) A (Other);

}

In fact, this sentence is very beautiful. On site sectors, but do not release memory, then locate constructors in placement New. From a technical point of view, it is not necessary. In practice, it works very well. It is a pity that sometimes beautiful is only one layer of skin! It is not unusual, there are three forms of exception security:

Basic guarantee: The element is still, but there may be possible state changes! No Resource Leak!

Strongly guarantee: either success or return to the starting point. Never let you keep two difficulties!

Do not throw an exception: certain success, will not fail!

The above operation is not achieved by the basic guarantee! If the Copy Constructionor fails, the invariance of the class may be destroyed! But now there is a better solution to create a temporary substitute, then exchange with * this. (Copy and Swap) created a member function swap () and guaranteed that its exception specification is throw (), which can be done by using the PIMPL. (You can refer to the Guru of The Week in www.gotw.ca).

However, if you guarantee that the Copy Constructor is successful, the other operations are normal. It should be no Object Slice, even if there is a derived. Because This-> ~ A () is definitely called from the destructor of the leaf (the farthest class), until the root class. [Note 4].

Note 4: Please refer to the ANSI C standard 12.4.

The answer to this question is not complete enough, in order to seek thorough answers, I still need to work hard, I can only do it above when writing this article. I have recently discovered Item 41. Object Lifetimes Part 2 in "Exceptional C " written by Herb Sutter, you can refer to it.

Now we should be able to pick it up. The answer to these five questions I hope everyone can add.

A little suggestion for beginners:

In our duckweed era, there is only duckweed life. Really able to learn something seriously, people doing things are really a phoenix! So the first thing to be able to have a correct mentality, you can't reach it, you will not change the age! Sometimes I see that people seem quickly, that is because I didn't see the cost of the incoming cost, if our foundation is good, a linguus is very good, and another will be faster. getting Started. Do you care about the operating system, compile principles, discrete mathematics ...? Do you carefully do a program? This way is hard to walk hard. Now the biggest shortcomings have always been encountered by teachers who can bring me a huge impact, and there is a basis too bad (professional relationship with the computer).

Second, we should also have a tolerant mentality. Can we tolerate different sounds constantly echo in the ear? When we argued with others, we can first stand in the brain before you publish your own point of view, think about why he thinks, why there is such views. When I want to say my own point of view, I will have the ability to convince others!

Finally, I want to learn a basic grammar when I learn a language, and the use of the standard library, don't be too early to deal with the devil (detail), otherwise it is easy to enter the magic! With the actual project, there is no subject, you can also think about writing String, various data structures, basic algorithms are also very harvest. Remember that the learning of expertise is very important, it is very important! Language is sometimes just a tool! Learning programming has a good way to read the program, write the program. Be sure to write! Wu Tong wrote in 2003.4.26

Recently modified 2003.6.16

Recently modified 2003.6.15

转载请注明原文地址:https://www.9cbs.com/read-6143.html

New Post(0)