Functions and usage of double pointers

Source: Internet
Author: User

Double pointers are mostly used for pointer exchange, which can avoid data replication and improve system performance. At the same time, you can also let the function modify the pointer, such as expanding its size and pointing

Generally, pointers are used as parameters. Most of them are used when the function needs to change the pointer (re-reference the variable) and cannot be passed through the return value (for example, the return value is used to pass other results.

In order to completely solve the problem of dynamic memory transfer, first review the knowledge points of memory management.

(1) There are three memory allocation methods:

● Allocated from the static storage area. When the program is compiled, it is allocated. This program exists throughout the entire running period. For example, global variables and static variables.

● Create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.

● Allocate from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us and the usage is flexible.

(2) pointer operation process

Apply for and initialize or configure NULL: int * pint = NULL;

Open a space or point it to an object: pint = new int (3); or Int I = 3; pint = & I;

Use a pointer (more specifically, to operate the memory, add If (pint! = NULL) or assert (pint! = NULL) before use, in case the memory application fails to use the pointer): If (P! = NULL) {use pint };

Release the used memory. Free (pint );

Set pointer to null pint = NULL; (avoid the emergence of wild pointer)

(3) In parameter passing of a function, the compiler always creates a temporary copy for each parameter of the function. If the parameter is P, the compiler will generate a copy of P _ p, make _ p = P. If the program in the function body modifies the content of _ p, the content of parameter P is modified accordingly. This is why pointers can be used as output parameters.

Example 1:

Void sample_alloc_safe_str_1 (char ** lppsz, unsigned int Len)

{* Lppsz = new char [Len + 1];}

Void main ()

{

Char * lpsz = NULL;

Sample_alloc_safe_str_1 (& lpsz, 100 );

Assert (lpsz! = NULL); // No assert

...}

//////////////////////////////////////// /////////////////////

Void sample_alloc_safe_str_2 (char * lpsz, unsigned int Len)

{

Lpsz = new char [Len + 1];

}

Void main ()

{

Char * lpsz = NULL;

Sample_alloc_safe_str_2 (& lpsz, 100 );

Assert (lpsz! = NULL); // assert!

...}

Example 2

Void Foo (INT ** pp)

{// Use ** pp * PP = new int (10 );}.

Int * P; // use P Foo (& P );

If you do not need a pointer, you can only

Int * Foo (int * P)

{// Use * P return New int (10 );}

Int * P; // use P

P = Foo (P );

Example 3

Void allocateint (int * PTR, int Len)

{Int I; PTR = (int *) malloc (LEN * sizeof (INT ));}

Allocateint (source, fsize );

The above function allocateint () uses a level-1 pointer to apply for memory in the function body. The result is allocateint (source, fsize). In this way, the value of source will never be changed, because the first-level pointer is passed by value, the variable in the stack exits when the function returns.

If you use this method, you can change it to the following:

Int * allocateint (INT Len)

{

Int * PTR = (int *)

Malloc (LEN * sizeof (INT ));

If (* PTR) = NULL)

Printf ("memory allocate Error !! ");

Return PTR; // heap memory, which will not be released when the function returns

}

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.