Pointers and dynamic storage allocations

Source: Internet
Author: User

1 Hands

Pointers are the basic concepts of the C language, and each data type has a corresponding pointer type. A pointer-type variable holds a value that is actually a memory address. The most basic operation of pointers:

& Take Address

* To reference (indirect reference)

Such as:

int I, *PI//Where I is an integer variable, and pi is a pointer to an integer.

PI = &i; Then &i returns the address of I and assigns it to PI, to assign a value to I, it can be written as I = 10; or *PI = 10; Pi before the * is to refer to, 10 is not stored in the pointer, but stored in the pi point to the storage unit.

Null//Represents a null pointer, does not point to a variable and does not point to a function, can be represented by an integer value of 0, the null in C is a macro, a constant value of 0 is defined, and a ' false ' can be represented in a relational expression

Test statement: if (pi = = NULL) or if (!PI)

2 Dynamic Storage Allocation

Program 1.1 allocating, freeing memory space

int I, *PI;

float F, *PF;

PI = (int *) malloc (sizeof (int)); There is a lack of judgment, and if there is insufficient storage space, it will fail.

PF = (float *) malloc (sizeof (float));

*PI = 1024;

*PF = 3.14;

printf ("An_integer_ = _%d,_a_float_ =_%f\n", *PI,*PF);

Free (PI);

Free (PF);

Replace the corresponding code for malloc:

if ((pi = (int*) malloc (sizeof (int))) = = NULL | | (PF = (float*) malloc (sizeof (float))) = = NULL)

{

Fprint (stderr, "insuffcient _ Memory");

Exit (Exit_fallure);

}

or use the following equivalent code:

if (! (  Pi = (int*) malloc (sizeof (int))) | | ! (PF = (float*) malloc (sizeof (Flota))))

{

Fprint (stderr, "insuffcient _ Memory");

Exit (Exit_fallure);

}

Because malloc is frequently used in programs, it is convenient to define macro statements

#define MALLOC (p,s) \

if (! ( p = malloc (s)))

{

Fprint (stderr, "insuffcient _ Memory");

Exit (Exit_fallure);

}

You can now replace the malloc statements in 1.1 programs

MALLOC (pi, sizeof (int));

MALLOC (PF, sizeof (float));

Insert a line after the 1.1 program printf statement:

PF = (float *) malloc (sizeof (float));

The current pointer points to a storage space that is no longer a storage unit that holds 3.14. You cannot access the original storage space at this time. This is an example of dangling references. As long as the pointer to the dynamic storage area is lost, the original storage area is lost to the program. One thing to keep in mind is that if you no longer need a dynamic storage space, be sure to return it to the system.

It is good practice to null all pointers that have not been pointed to in a C program.

Another good practice is that when you convert a pointer type, you should display the use coercion type conversion. Such as:

PI = malloc (sizeof (int)); Assign to pi a pointer to int

PF = (float*) pi; Casts an int pointer to a float pointer

Pointers and dynamic storage allocations

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.