High quality C ++/C Programming Guide-Memory Management 2

Source: Internet
Author: User
Memory Management

7.5 what about the pointer in free and delete?

[The original document is missing]

7.7 eliminate "wild"Pointer"

The "wild pointer" is not a NULL pointer, but a pointer to the "junk" memory. Generally, NULL pointers are not incorrectly used, because if statements are easy to judge. However, the "wild Pointer" is very dangerous, and the if statement does not work for it.

There are two main causes of "wild pointer:

(1) pointer variables are not initialized. When a pointer variable is created, it does not automatically become a NULL pointer. Its default value is random, which means it is random. Therefore, the pointer variable should be initialized at the same time when it is created, either set the pointer to NULL or set it to direct to the legal memory. For example

Char * p = NULL;

Char * str = (char *) malloc (100 );

(2) After the pointer p is free or deleted, It is not set to NULL, which makes people mistakenly think p is a valid pointer. See section 7.5.

(3) pointer operations go beyond the scope of variables. This situation is hard to prevent. The example program is as follows:

Class

{

Public:

Void Func (void)

{

Cout <"Func of class A" <endl;

}

};

Void Test (void)

{

A * p;

{

A;

P = & a; // note the life cycle of

}

P-> Func (); // p is a "wild pointer"

}

When the function Test executes the statement p-> Func (), object a has disappeared, and p points to a, so p becomes a "wild pointer ". But the strange thing is that I did not encounter any errors when running this program, which may be related to the compiler. (I am a bit confused. Can this be regarded as a wild pointer ?)

7.8 why new/delete is required when malloc/free is available?

Malloc and free are standard library functions in C ++/C, and new/delete are operators in C ++. They can be used to apply for dynamic memory and release memory.

For non-Internal data objects, maloc/free alone cannot meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free.

Therefore, the C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can clean up and release memory. Note that new/delete is not a database function.

First, let's take a look at how malloc/free and new/delete implement dynamic memory management of objects. See example 7-8.

Class Obj

{

Public:

Obj (void) {cout <"Initialization" <endl ;}

~ Obj (void) {cout <"Destroy" <endl ;}

Void Initialize (void) {cout <"Initialization" <endl ;}

Void Destroy (void) {cout <"Destroy" <endl ;}

};

Void UseMallocFree (void)

{

Obj * a = (obj *) malloc (sizeof (obj); // apply for dynamic memory

A-> Initialize (); // Initialization

//...

A-> destroy (); // clear the job

Free (a); // releases the memory.

}

Void usenewdelete (void)

{

OBJ * A = new OBJ; // apply for dynamic memory and initialize

//...

Delete A; // clear and release the memory

}

Example 7-8 How to Use malloc/free and new/Delete to manage dynamic memory of Objects

The class OBJ function initialize simulates the constructor function, and the function destroy simulates the destructor function. In usemallocfree, because malloc/free cannot execute constructor and destructor, you must call the member functions initialize and destroy to complete initialization and clearing. The usenewdelete function is much simpler.

Therefore, we should not attempt to use malloc/free to manage the memory of dynamic objects. We should use new/Delete. Because the internal data type "object" does not have a process of construction and analysis, malloc/free and new/delete are equivalent to them.

Since the new/delete function completely covers malloc/free, why does C ++ not eliminate malloc/free? This is because C ++ programs often call C functions, and C Programs can only use malloc/free to manage dynamic memory.

If you use free to release the "New Dynamic Object", this object may cause program errors because it cannot execute the destructor. If you use Delete to release the "dynamic memory applied by malloc", theoretically, the program will not go wrong, but the program is poorly readable. Therefore, new/delete must be paired, and the same applies to malloc/free.

7.9 What should I do if the memory is exhausted?

If a large enough memory block cannot be found when applying for dynamic memory, malloc and new will return a null pointer, declaring that the memory application failed. There are usually three ways to handle the "memory depletion" problem.

(1) judge whether the pointer is null. If yes, use the return statement to terminate the function immediately. For example:

Void func (void)

{

A * A = new;

If (A = NULL)

{

Return;

}

...

}

(2) judge whether the pointer is null. If yes, use exit (1) to terminate the entire program. For example:

Void func (void)

{

A * A = new;

If (A = NULL)

{

Cout <"Memory Exhausted" <Endl;

Exit (1 );

}

...

}

(3) set exception handling functions for new and malloc. For example, in Visual C ++, you can use the _ set_new_hander function to set your own exception handling function for new, or enable malloc to use the same exception handling function as new. For more information, see the C ++ user manual.

The above (1) (2) method is the most common. If a function needs to apply for dynamic memory in multiple places, the method (1) is insufficient (it is troublesome to release the memory) and should be handled in the way (2.

A lot of people cannot bear to use exit (1). They asked, "Can the operating system solve the problem by itself without writing error handling programs ?"

No. In case of a "memory depletion" event, generally applications are no longer saved. If you do not use exit (1) to kill the program, it may kill the operating system. The truth is: if you do not kill a gangster, the gangster will commit more crimes before he dies.

There is a very important phenomenon to tell you. For 32-bit applications, no matter how malloc and new are used, it is almost impossible to cause "memory depletion ". In Windows 98, I wrote a test program using Visual C ++. See example 7-9. This program will run endlessly and will not be terminated at all. Because the 32-bit operating system supports "virtual storage" and the memory is used up, the hard disk space is automatically replaced. I only heard the sound of the hard drive. Window 98 was so tired that it didn't respond to the keyboard or mouse.

I can conclude that for 32-bit or more applications, the "memory used up" error handler is useless. Now, Unix and Windows programmers are happy with this: the error handler does not work, and I will not write it, saving a lot of trouble.

I don't want to mislead readers. I must emphasize that without error handling, the quality of the program will be poor, and never be compromised.

Void main (void)

{

Float * p = NULL;

While (TRUE)

{

P = new float [1000000];

Cout <"eat memory" <endl;

If (p = NULL)

Exit (1 );

}

}

Example 7-9 try to exhaust the operating system memory

7.10 usage of malloc/free

The following is a prototype of the malloc function:

Void * malloc (size_t size );

Use malloc to apply for an integer-type memory with a length. The program is as follows:

Int * p = (int *) malloc (sizeof (int) * length );

We should focus on two elements: "type conversion" and "sizeof ".

The type returned by malloc is void *. Therefore, you must explicitly convert the type when calling malloc and convert void * to the required pointer type.

The malloc function does not recognize the type of memory to be applied for. It only cares about the total number of bytes in the memory. We usually cannot remember the exact number of bytes for int, float, and other data types. For example, the int variable is 2 bytes in a 16-bit system and 4 bytes in a 32-bit system, and the float variable is 4 bytes in a 16-bit system, it is also 4 bytes in 32 bits. It is best to use the following program for a test:

Cout <sizeof (char) <Endl;

Cout <sizeof (INT) <Endl;

Cout <sizeof (unsigned INT) <Endl;

Cout <sizeof (long) <Endl;

Cout <sizeof (unsigned long) <Endl;

Cout <sizeof (float) <Endl;

Cout <sizeof (double) <Endl;

Cout <sizeof (void *) <Endl;

Using the sizeof operator in malloc's "()" is a good style, but be careful when we sometimes get dizzy and write P = malloc (sizeof (p )) such a program.

The prototype of function free is as follows:

Void free (void * memblock );

Why isn't the free function as complicated as the malloc function? This is because the pointer p type and the memory capacity it refers to are known in advance, and the statement free (p) can correctly release the memory. If P is a null pointer, no matter how many times the free P operation will fail. If P is not a null pointer, the free operation on P will cause a program running error.

7.11 usage highlights of new/delete

The new operator is much easier to use than the malloc function, for example:

Int * P1 = (int *) malloc (sizeof (INT) * length );

Int * P2 = new int [length];

This is because new has built-in sizeof, type conversion, and type security check functions. For non-Internal data objects, new completes initialization while creating dynamic objects. If an object has multiple constructors, the new statement can also have multiple forms. For example

Class OBJ

{

Public:

OBJ (void); // a constructor without Parameters

OBJ (int x); // constructor with a parameter

...

}

Void test (void)

{

OBJ * A = new OBJ;

OBJ * B = new OBJ (1); // The initial value is 1.

...

Delete;

Delete B;

}

If you use new to create an object array, you can only use the non-parameter constructor of the object. For example

OBJ * objects = new OBJ [100]; // create 100 Dynamic Objects

Cannot be written

OBJ * objects = new OBJ [100] (1); // create 100 dynamic objects and assign initial value 1

When releasing an object Array Using Delete, do not lose the symbol '[]'. For example

Delete [] objects; // correct usage

Delete objects; // incorrect usage

The latter is equivalent to delete objects [0], and 99 Other objects are missing.

7.12 experiences

I know many well-developed C ++/C programmers. Few people can pat their chests and say they are familiar with pointer and memory management (including myself ). When I first learned the C language, I was so stupid that I didn't use a pointer when I was developing the first application software (about 10 thousand lines of C code). I used arrays to replace pointers. It was not a solution to avoid pointers. Later I changed the software and reduced the amount of code to half of the original one.

My lessons are:

(1) The more you are afraid of pointers, the more you need to use pointers. If pointer is not used correctly, it is definitely not a qualified programmer.

(2) You must develop the habit of gradually tracing programs using the debugger. Only in this way can you discover the essence of the problem.

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.