C ++ memory. Double pointer, advanced usage of pointer

Source: Internet
Author: User

There are three memory allocation methods:
(1) distribution from the static storage area. It is allocated when the program is compiled.
It exists throughout the running period. For example, global variables and static variables.
(2) create a stack. When executing a function, all the storage units of local variables in the function can be created on the stack.
These storage units are automatically released when execution ends. Stack memory allocation operations are embedded in the processor's Instruction Set
Medium, high efficiency, but the allocated memory capacity is limited.
(3) allocate from the stack, also known as dynamic memory allocation. When the program is running, use malloc or new to apply for any number
With less memory, the programmer is responsible for releasing the memory with free or delete. Lifetime of dynamic memory
It is up to us to decide that it is very flexible to use, but there are also the most problems.

?? The memory allocation fails, but it is used.
New programmers often make this mistake because they do not realize that memory allocation will fail. The common solution is,
Check whether the pointer is null before using the memory. If the pointer P is a function parameter
Use assert (P! = NULL. If you use malloc or new to apply for memory, you should use if (P = NULL)
Or if (P! = NULL.

?? Although the memory allocation is successful, it is referenced before initialization.

?? The memory allocation is successful and initialized, but the operation is beyond the memory boundary.

?? I forgot to release the memory, causing memory leakage.
A function containing such errors loses a piece of memory every time it is called. At the beginning, the system had enough memory. You
You cannot see the error. Once a program suddenly died, the system prompts: memory is exhausted.
Dynamic Memory application and release must be paired. The usage of malloc and free in the program must be the same, no
Then there must be an error (the same is true for new/delete ).

?? The memory is released but it continues to be used.
There are three scenarios:
(1) The object calling relationship in the program is too complex, so it is difficult to figure out whether an object has been released
At this time, we should re-design the data structure to fundamentally solve the chaos of Object Management.
(2) The Return Statement of the function is incorrect. Be sure not to return the "Pointer" or "Reference" pointing to "stack memory ",
This is because the function body is automatically destroyed when it ends.
(3) After the memory is released using free or delete, the pointer is not set to null. Resulting in "wild pointer"

 

How does the pointer Parameter Pass the memory?
If the function parameter is a pointer, do not expect this pointer to apply for dynamic memory.

The test function's statement getmemory (STR, 200) does not enable STR to obtain the expected 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 is still null
Strcpy (STR, "hello"); // running error
}

The fault lies in the getmemory function. The compiler always needs to make a temporary copy for each parameter of the function, pointer
The copy of parameter P is _ p, and the compiler makes _ p = P. If the program in the function body modifies the content of _ p
Modify the content of parameter P. This is why pointers can be used as output parameters. In this example, _ P applies
The new memory is changed, but the memory address indicated by _ p is not changed at all. So the getmemory Function
It cannot output anything. In fact, each execution of getmemory exposes a piece of memory, because it is useless.
Free releases memory.
If you have to use pointer parameters to request memory, you should use "pointer to Pointer" instead"
Void getmemory2 (char ** P, int num)
{
* P = (char *) malloc (sizeof (char) * num );
}
Void Test2 (void)
{
Char * STR = NULL;
Getmemory2 (& STR, 100); // note that the parameter is & STR, not Str
Strcpy (STR, "hello ");
Cout <STR <Endl;
Free (STR );
}

Because the concept of "pointer to Pointer" is not easy to understand, we can use function return values to transmit dynamic
Memory. This method is simpler.
Char * getmemory3 (INT num)
{
Char * P = (char *) malloc (sizeof (char) * num );
Return P;
}
Void test3 (void)
{
Char * STR = NULL;
STR = getmemory3 (100 );
Strcpy (STR, "hello ");
Cout <STR <Endl;
Free (STR );
}
Although it is easy to use the function return value to pass dynamic memory, some people often use the return statement in an error.
. It is emphasized that the return statement should not be used to return the pointer pointing to the "stack memory", because there is a function end time in it.
Automatic extinction
Char * getstring (void)
{
Char P [] = "Hello World ";
Return P; // the compiler will give a warning
}
Void test4 (void)
{
Char * STR = NULL;
STR = getstring (); // STR content is junk
Cout <STR <Endl;
}
Use the debugger to track test4 step by step. After executing the STR = getstring statement, STR is no longer a null pointer,
However, STR content is not "Hello World" but garbage.
Char * getstring2 (void)
{
Char * P = "Hello World ";
Return P;
}
Void test5 (void)
{
Char * STR = NULL;
STR = getstring2 ();
Cout <STR <Endl;
}
Although the function test5 runs without errors, the design concept of the function getstring2 is incorrect. Because
The "Hello World" in getstring2 is a constant string located in the static storage zone. It is within the lifetime of the program.
Constant. No matter when getstring2 is called, it returns the same read-only memory block.

Original article: http://www.cppblog.com/mzty/archive/2005/11/09/1004.html

Related Article

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.