Several questions about pointers and memory

xiaoxiao2021-03-06  65

First, "DELETE P" will delete the "P" pointer or the information it refers to, "* p"?

The information indicated by the pointer. "Delete" actually means: "Delete the Thing Pointed to by). The same English misuse also occurs in the memory pointed to the "release" indicator ("Free (P)" true meaning: "free_the_stuff_pointed_to_by (p)").

Second, can "free ()" is configured by "New", "DELETE" removing the memory configured by "Malloc ()"?

Not. In the same program, using malloc / free and new / delete is completely legal, reasonable, safe; but free off is configured by New, or delete is not legally configured by Malloc, unreasonable.

Third, why should I use "new" instead of malloc ()?

Constructing sub / decimalizes, type security, OverridAbility. Construct Sub / Deconstructure: and "Malloc (SIZEOF (FRED)" "" New Fred () "also calls Fred's constructors. Similarly, "Delete P" will call "* p" deconstruction. Typical security: malloc () will pass a "VOID *" that does not have certainly securely, and "New Fred ()" will pass back the correct type indicator (a "fred *"). Override: "New" is an operator that can be covered by the object category, and "Malloc" is not a baseline that is covered by "various categories".

Fourth, why C does not match "new" and "DELETE" with "Realloc ()"?

Avoid you cause accident. When Realloc () is to copy the configuration area, it is done the copy of "Charter BitWise", which can break the large part of C objects. However, C objects should be able to copy themselves: Use their own copy to construct a sub-or set operator.

5. How to configure / release arrays?

Use new [] and delete []:

Fred * p = new fred [100]; // ... delete [] p;

Whenever you use "[...]" in the "New" operation, you will *! *! * Use "[]" in the "DELETE" statement. This syntax is necessary because "indicators to a single element" and "indicators pointing to an array" are unable to distinguish between syntax.

Sixth, in case I forgot "[]" what happens in the "DELETE" by "New Fred [N]"?

disaster. This is the programmer - not the compiler - responsibility to ensure the correct pairing of New [] and Delete []. If you make a mistake, the compiler does not generate an error message for any compile period or execution period. Stacking (HEAP) is destroyed is the most likely ending, or worse, your program will be turned off. 7. The action of the member function is "DELETE THIS" is legal (and is it good)?

As long as you are careful, there is fine. The so-called "careful" is: 1) You get 100% to determine "this" is configured by "New" (not "new []", also non-policy "new" version, must be the most original " "). 2) You have 100% to determine that the member function is the last one of this object will call. 3) After the action of suicide ("delete this;"), you can't touch the object of "this", including information and operational behavior. 4) After completing the action of suicide ("delete this;"), you can't touch the "this" indicator. In other words, you can't view it, compare it with other indicators or NULL, print its value, transform it, do anything to it.

Naturally, this warning also applies to: When "this" is a indicator pointing to the base category, the deconstruction is not a Virtual.

8. How to use new to configure a multi-dimensional array?

There are many ways to terminate your requirements for the scalability of the array size. Extremely, if you know the dimension of all arrays in the compile period, you can configure (just like C):

Class Fred {/*...*/};

Void manipulateArray () {Fred Matrix [10] [20];

// Use Matrix [i] [j] ...

// Do not allow special release of this array}

Another extreme situation, if you want each small piece of the matrix, you can configure it in the free memory:

Void ManipulateArray (unsigned nRows, unsigned ncols [] // 'nROWS' is the number of columns of the array. // So the legal column number is (0, nROWS-1) open section. // 'ncols [r]' is the number of rows of 'R' columns ('R' value fields [0..nrows-1]). {FRED ** Matrix = new fred * [nROWS]; for (unsigned r = 0; r

// Use Matrix [i] [j] ...

// Release is the reaction of the configuration: for (r = nrows; r> 0; --r) delete [] matrix [r-1]; delete [] matrix;}

Nine, how to ensure that some items are built with "new" instead of zone or overall / static variable?

Determining the category of this category is "private:", and define a "friend" or "static" function to pass back the object to be built by "New" (set the construction of the homology "protected:", If you want to have a derivative category). Class Fred {// only allows Fred dynamically configured public: static fred * create () {return new fred ();} static fred * create (INT i) {Return New Fred (i);} static fred * create Fred & fred); 4.} private: fred (); fred (int i); fred (const fred & fred);

Main () {fred * p = fred :: Create (5); ... delete p;

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

New Post(0)