[C ++] Millions Carefully Local Variables

zhaozj2021-02-16  52

Ten million careful local variables

Be careful to optimize your local variables, especially in this case: When you declare an object in a Function, then you spare no effort to processed this object, do it. Ok, compile, no problem, you seem to be satisfied. But don't be happy too early, try it, oh god, memory quote error, the operating system has a very cute little red fork, but you can't wait to "one hundred times". Have you had this experience? Well, good, let us see the following code that is enough to produce such an effect:

#include

#include

Using namespace std;

Class string {public: String () {}

String (char * str) {size_t len ​​= strlen (str); p = new char [len 1]; struct (p, str);

String (const string & r Hs) {size_t len ​​= strlen (rhs.p); p = new char [len 1]; strcpy (p, rhs.p);}

Void Print () {char * pp = p; while (* pp! = '/ 0') {cout << * pp; pp;}}

CHAR * P;};

INT main () {String STR1 ("Hello, World! / N"); string str2 (str1); str1.print (); str2.print (); return 0;}

Well ~, the program simulates the behavior of String, is it simple? Running, it seems good. Ok, I want to do this now, my String: String str3; str3 = str1 str2; what should I do? Very natural idea is to add an overload function for the operator " " for my program. Write this way, see if there is a problem:

String Operator {string Temp; size_t len ​​= strlen (p) strlen (rhs.p); Temp.p.p = new char [len]; strcpy (temp.p, p); strcat (Temp. p, rhs.p); return temp;} Replace the original main function in the following main:

INT main () {String STR1 ("Hello, World!"); String STR2 (STR1); String Str3; Str3 = Str1 Str2 ;? Cout << "/ nStr1:"; str1.print (); cout << "/ nStr2:"; str2.print (); cout << "/ nstr3:"; str3.print (); cout << end1; return 0;}

Run, good, no problem! Let's take a look at our procedure, what? Do you find that we have not released after the memory from the pile of NEW! How can this? ! So, naturally, we added a destructor to our program:

~ String () {delete [] p;}

Compile ..., ok! no problem! Run ..., oh god! what's happenin? Small red fork! ! ! At the beginning of the article, I finally happened, did you look forward to a long time. Oh, you may have discovered that the problem is in the operator overload function because he returns a local variable. When this partial variable is transmitted, he is destroyed in the operator overload function. So it can be judged that this assignment number "=" does not copy one by one in the object to the target. Therefore, one means is to overload "=", and another simple method is to declare the STR3 as: str3: String Str3 = STR1 STR2; thus directly using the copy constructor to complete this task, try, the program is Isn't it possible to run? Let's take a look at another solution, let's overload "=", we add the following code to our program:

Void Operator = (const string & r Hs) {size_t len ​​= strlen (rhs.p); p = new char [len 1]; strcpy (p, rhs.p);}

Compilation and running, huh, our procedure is restored! So, we see that in a Function returns a local variable, he will pay itself before he is destroyed, thereby assigning the content to the target. So you need to overload "=" and provide copy constructive; if you try to return a pointer to a local variable in a Function, then you are buddy. This pointer Copy itself before he is destroyed (Note this is an address information), but the target once again wants to visit him again, his original address has been destroyed, our goal is not to get his specific information, we It was only one of his address information at the time. The focus is: Pass-by-value is the consent of Copy Constructor; Initialization initialization is completed by the constructor, and the assignment assignment is completed by Operator =. So, until this, everything is clean ...

A small rookie, I don't know how the sky is thick, and I will have a good job. Welcome everyone to shoot bricks!

Sun

2004-7-15

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

New Post(0)