Step into the minefield of C ++-detailed explanation of C ++ memory management (IV)

Source: Internet
Author: User
8. 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.

  9. Usage 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.

  10. Some 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.