C ++ memory related

xiaoxiao2021-03-06  21

--------- Excerpt from "High Quality C Programming Guide" -----------

1 How does the pointer parameter pass memory?

If the parameter of the function is a pointer, do not expect to use the pointer to apply for dynamic memory. Examples 7-4-1, the statement of the TEST function GetMemory (STR, 200) did not enable Str to get the desired memory, STR is still null, why?

Void getMemory (CHAR * P, INT NUM) {p = (char *) malloc (sizeof (char) * NUM);} void test (void) {char * str = null; getMemory (STR, 100); // STR Still null struct (str, "hello"); // run error}

Example 7-4-1 Attempt to apply for dynamic memory with pointer parameters

The problem is in the function GetMemory. The compiler always makes a temporary copy for each parameter of the function, the copy of the pointer parameter P is _P, the compiler makes _p = P. If the program in the function is modified, the contents of the parameter P are modified accordingly. This is why the pointer can be used as an output parameter. In this example, _P applies for new memory, just changing the memory address referred to in _P, but p is unchanged. So the function getMemory does not output anything. In fact, every time GetMemory will disclose a memory because free memory is released.

If you have to apply for memory with a pointer parameter, you should use the "pointer to the pointer", see Examples 7-4-2.

Void getMemory2 (char ** p, int num) {* p = (char *) malloc (sizeof (char) * NUM);} void test2 (void) {char * str = null; getMemory2 (& STR, 100); / / Note that the parameters are & STR instead of Str struct (STR, "Hello"); cout << str << endl; free (str);}

Example 7-4-2 Apply Dynamic Memory with a pointer to a pointer

Due to the concept of "pointers" pointing pointers ", we can use function return values ​​to pass dynamic memory. This method is more simple, see Examples 7-4-3.

Char * getMemory3 (int NUM) {char * p = (char *) malloc (sizeof (char) * num); returnit p;} void test3 (void) {char * str = null; str = getMemory3 (100); strcpy (STR, "Hello"); cout << str << endl; free (str);}

Example 7-4-3 Return value with a function to pass dynamic memory

This method is transmitted with a function return value to transmit dynamic memory. Although it is easy to use, someone often uses the returnite sentence. Here, it emphasizes not to return to the "stack memory" pointer to the "stack memory", because the internal existence of the function is automatically done, see Examples 7-4-4.

Char * getString (void) {char p [] = "Hello World"; return p; // The compiler will make a warning} void test4 (void) {char * str = null; str = getString (); // STR The content is junk cout << str << Endl;} example 7-4-4 RETURN statement returns a pointer to "stack memory"

Test4 with the debugger, find the Str = getString statement, Str is no longer NULL pointer, but the content of the STR is not "Hello World" but also garbage.

If you rewrite the sample 7-4-4, what is it?

Char * getString2 (void) {char * p = "Hello World"; returnom} void test5 (void) {char * str = null; str = getString2 (); cout << str << endl;}

Example 7-4-5 Return statement Returns a constant string

Although the function test5 is running will not be wrong, the design concept of the function getString2 is wrong. Because "Hello World" in getString2 is a constant string, located in a static storage area, it is constant in the program life period. Whenever you call getString2, it returns to the same "read-only" memory block.

2 "wild pointer" is not NULL pointer, is a pointer to "garbage" memory. People generally don't miss the NULL pointer because it is easy to judge with the IF statement. But "wild pointer" is very dangerous, if the IF statement does not work.

There are two main cations of "wild pointers":

(1) The pointer variable is not initialized. Any pointer variable is not automatically become a NULL pointer when it is created, and its default is random, it will finish it. So, the pointer variable should be initialized while creating, or the pointer is set to null, or it points to the legitimate memory. E.g

CHAR * P = NULL;

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

(2) The pointer P is after free or delete, it is not set to NULL, making it mistaken to P is a legal pointer. See Section 7.5.

(3) The pointer operation exceeds the scope of the variable. This situation makes people defense, the sample program is as follows:

Class A

{

PUBLIC:

Void func (void) {cout << "FUNC OF Class A" << endl;}

}

Void test (void)

{

A * p;

{

A a a;

P = & a; // Note A life

}

P-> func (); // P is "wild pointer"

}

Function TEST When performing statement P-> FUNC (), the object A has disappeared, and P is pointing A, so P is "wild pointer". But strange is that I have no error when I run this program, which may be related to the compiler.

3malloc and free are standard library functions for C / C language, and new / delete is C operator. They can be used to apply for dynamic memory and release memory.

For objects for non-internal data types, light MALOC / FREE cannot meet the requirements of dynamic objects. Objects are created automatically execute constructor automatically, and the object is automatically executed before erophan. Since Malloc / Free is a library function instead of an operator, it is not possible to impose the task of performing constructor and the destructuring function within Malloc / Free. Therefore, the C language requires a operator New, which can complete the dynamic memory allocation and initialization, and an operator DELETE that can complete the cleaning and release of memory. Note NEW / Delete is not a library function.

The prototype of the function free is as follows:

Void Free (Void * MEMBLOCK);

Why is the free function is not as complicated as the malloc function? This is because the type of pointer P and the capacity of the memory it refers to in advance, and the statement free (p) can release memory correctly. If P is a null pointer, Free will not have problems with the operation of P no matter how many operations. If the P is not a NULL pointer, the FREE will cause the program to run errors twice.

The prototype of the function malloc is as follows:

Void * malloc (size_t size);

Apply with Malloc to the integer type of the length of length Length, the program is as follows:

INT * P = (int *) malloc (sizeof (int) * length);

We should focus on two elements: "Type Conversion" and "SIZEOF".

U Malloc returned value is Void *, so the type conversion is to be explicitly performed when calling Malloc, converting VOID * to the required pointer type.

The u malloc function itself does not recognize what type of memory to be applied, it only cares about the total number of bytes of memory. We can never remember int, Float and other data types of variables of the exact byte number. For example, the int variable is 2 bytes under the 16-bit system, and below 32 is 4 bytes; and the float variable is 4 bytes under the 16-bit system, and below 32 is 4 bytes.

Void * malloc (size_t size);

Apply with Malloc to the integer type of the length of length Length, the program is as follows:

INT * P = (int *) malloc (sizeof (int) * length);

We should focus on two elements: "Type Conversion" and "SIZEOF".

U Malloc returned value is Void *, so the type conversion is to be explicitly performed when calling Malloc, converting VOID * to the required pointer type.

The u malloc function itself does not recognize what type of memory to be applied, it only cares about the total number of bytes of memory. We can never remember int, Float and other data types of variables of the exact byte number. For example, the int variable is 2 bytes under the 16-bit system, and below 32 is 4 bytes; and the float variable is 4 bytes under the 16-bit system, and below 32 is 4 bytes.

Void Free (Void * MEMBLOCK);

Why is the free function is not as complicated as the malloc function? This is because the type of pointer P and the capacity of the memory it refers to in advance, and the statement free (p) can release memory correctly. If P is a null pointer, Free will not have problems with the operation of P no matter how many operations. If the P is not a NULL pointer, the FREE will cause the program to run errors twice.

The prototype of the function malloc is as follows:

Void * malloc (size_t size);

Apply with Malloc to the integer type of the length of length Length, the program is as follows:

INT * P = (int *) malloc (sizeof (int) * length);

We should focus on two elements: "Type Conversion" and "SIZEOF". U Malloc returned value is Void *, so the type conversion is to be explicitly performed when calling Malloc, converting VOID * to the required pointer type.

The u malloc function itself does not recognize what type of memory to be applied, it only cares about the total number of bytes of memory. We can never remember int, Float and other data types of variables of the exact byte number. For example, the int variable is 2 bytes under the 16-bit system, and below 32 is 4 bytes; and the float variable is 4 bytes under the 16-bit system, and below 32 is 4 bytes.

Void * malloc (size_t size);

Apply with Malloc to the integer type of the length of length Length, the program is as follows:

INT * P = (int *) malloc (sizeof (int) * length);

We should focus on two elements: "Type Conversion" and "SIZEOF".

U Malloc returned value is Void *, so the type conversion is to be explicitly performed when calling Malloc, converting VOID * to the required pointer type.

The u malloc function itself does not recognize what type of memory to be applied, it only cares about the total number of bytes of memory. We can never remember int, Float and other data types of variables of the exact byte number. For example, the int variable is 2 bytes under the 16-bit system, and below 32 is 4 bytes; and the float variable is 4 bytes under the 16-bit system, and below 32 is 4 bytes.

For objects for non-internal data types, light MALOC / FREE cannot meet the requirements of dynamic objects. Objects are created automatically execute constructor automatically, and the object is automatically executed before erophan. Since Malloc / Free is a library function instead of an operator, it is not possible to impose the task of performing constructor and the destructuring function within Malloc / Free.

Therefore, the C language requires a operator New, which can complete the dynamic memory allocation and initialization, and an operator DELETE that can complete the cleaning and release of memory. Note NEW / Delete is not a library function.

The prototype of the function free is as follows:

Void Free (Void * MEMBLOCK);

Why is the free function is not as complicated as the malloc function? This is because the type of pointer P and the capacity of the memory it refers to in advance, and the statement free (p) can release memory correctly. If P is a null pointer, Free will not have problems with the operation of P no matter how many operations. If the P is not a NULL pointer, the FREE will cause the program to run errors twice.

The prototype of the function malloc is as follows:

Void * malloc (size_t size);

Apply with Malloc to the integer type of the length of length Length, the program is as follows:

INT * P = (int *) malloc (sizeof (int) * length);

We should focus on two elements: "Type Conversion" and "SIZEOF".

U Malloc returned value is Void *, so the type conversion is to be explicitly performed when calling Malloc, converting VOID * to the required pointer type.

The u malloc function itself does not recognize what type of memory to be applied, it only cares about the total number of bytes of memory. We can never remember int, Float and other data types of variables of the exact byte number. For example, the int variable is 2 bytes under the 16-bit system, and below 32 is 4 bytes; and the float variable is 4 bytes under the 16-bit system, and below 32 is 4 bytes.

Void * malloc (size_t size);

Apply with malloc to the integer type of the length of length Length, the program is as follows: int * p = (int *) malloc (intend * length);

We should focus on two elements: "Type Conversion" and "SIZEOF".

U Malloc returned value is Void *, so the type conversion is to be explicitly performed when calling Malloc, converting VOID * to the required pointer type.

The u malloc function itself does not recognize what type of memory to be applied, it only cares about the total number of bytes of memory. We can never remember int, Float and other data types of variables of the exact byte number. For example, the int variable is 2 bytes under the 16-bit system, and below 32 is 4 bytes; and the float variable is 4 bytes under the 16-bit system, and below 32 is 4 bytes.

Void Free (Void * MEMBLOCK);

Why is the free function is not as complicated as the malloc function? This is because the type of pointer P and the capacity of the memory it refers to in advance, and the statement free (p) can release memory correctly. If P is a null pointer, Free will not have problems with the operation of P no matter how many operations. If the P is not a NULL pointer, the FREE will cause the program to run errors twice.

The prototype of the function malloc is as follows:

Void * malloc (size_t size);

Apply with Malloc to the integer type of the length of length Length, the program is as follows:

INT * P = (int *) malloc (sizeof (int) * length);

We should focus on two elements: "Type Conversion" and "SIZEOF".

U Malloc returned value is Void *, so the type conversion is to be explicitly performed when calling Malloc, converting VOID * to the required pointer type.

The u malloc function itself does not recognize what type of memory to be applied, it only cares about the total number of bytes of memory. We can never remember int, Float and other data types of variables of the exact byte number. For example, the int variable is 2 bytes under the 16-bit system, and below 32 is 4 bytes; and the float variable is 4 bytes under the 16-bit system, and below 32 is 4 bytes.

Void * malloc (size_t size);

Apply with Malloc to the integer type of the length of length Length, the program is as follows:

INT * P = (int *) malloc (sizeof (int) * length);

We should focus on two elements: "Type Conversion" and "SIZEOF".

U Malloc returned value is Void *, so the type conversion is to be explicitly performed when calling Malloc, converting VOID * to the required pointer type.

The u malloc function itself does not recognize what type of memory to be applied, it only cares about the total number of bytes of memory. We can never remember int, Float and other data types of variables of the exact byte number. For example, the int variable is 2 bytes under the 16-bit system, and below 32 is 4 bytes; and the float variable is 4 bytes under the 16-bit system, and below 32 is 4 bytes.

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

New Post(0)