C Primer Plus (12)

Source: Internet
Author: User

12.1 Storage Class

Storage Period: the time when the variable is stored in the memory. The scope and link of the variable indicate which parts of the program can use the variable name.
Different storage classes provide variable scopes, links, and different combinations of storage periods.

12.1.1 Scope

The scope describes one or more regions in a program that can access an identifier. The scope of a C variable can be code block scope, function prototype scope, or file scope.
A variable defined in a code block has a code block scope, from where the variable is defined to the end of the code block that contains the definition. In addition, although the formal parameters of a function are defined before the start of the function, they also have a code block scope and are affiliated to the code block containing the function body.
Traditionally, variables with code block scopes must be declared at the beginning of the code block. C99 relaxed this restriction, allowing variables to be declared anywhere in a code block.
The function prototype scope ranges from the definition of a variable to the end of the prototype declaration. This means that when processing a function prototype parameter, the compiler only cares about the type of the parameter, rather than the parameter name. (Except VLA)
A variable defined outside of all functions has a file scope. It is visible from the definition to the end of the file containing the definition.

There is also a scope called function scope, but it only applies to the labels used by the goto statement.

12.1.2 Link

A c variable has one of the following links: External link, internal link, or empty link.
Variables with code block scopes or function prototype scopes have null links, meaning they are private to the code block or function prototype they define.
Variables with file scopes may have internal or external links.
A variable with external links can be used anywhere in a multi-file program.
A variable with internal links can be used anywhere in a file.

12.1.3 Storage Period

If a variable has a static storage period, it will always exist during program execution. Variables with file scopes have static storage periods.
All file-scope variables, whether internal or external, have static storage periods.

12.1.4 automatic Variables

By default, any variables defined in the Code block scope or function header belong to the Automatic Storage Class. You can also use the keyword auto to make your intention clearer.
If a statement is a part of a loop or if statement, it is considered a code block even if {} is not used.

12.1.5 register variable

Declaring a register variable is only a request rather than a direct command. Therefore, it cannot be achieved, but it becomes a common automatic variable.
The types that can be declared using register are limited. For example, the processor may not have enough registers to accommodate the double type.

12.1.6 static variables with code block scopes

Static variables and external variables are in place when the program calls into the memory.

12.1.7 static variables with external links

Put the definition declaration of variables outside all functions, that is, an external variable is created. To make the program clearer, you can use the extern keyword to declare it again. If the variable is defined in another file, it is necessary to use extern to declare the change volume.
If extern is missing in the function declaration, an independent automatic variable is created.
External variable scope: starts from the declared position to the end of the file.
Like automatic variables, external variables can be initialized explicitly. Unlike automatic variables, if you do not initialize external variables, they are automatically assigned 0 values.
Main ()
{
External int tern;
The first declaration of tern is the definition declaration, and the second declaration is the reference declaration.
Do not use the keyword extern for external definition. Use it to reference an existing external definition.
An external variable can be initialized only once, and must be implemented when the variable is defined.
Extern char permis = 'U'; // Error

12.1.8 static variables with internal links

It can only be used by functions in the same file as it.

12.2 Storage Class specifiers

The C language contains five keywords used as storage class specifiers: auto, register, static, extern, and typedf. Typedf has nothing to do with memory storage and is classified as this type due to syntax reasons. One or more storage class specifiers cannot be used in one speaker.
Auto: a variable has an automatic storage period. This specifier can only be used in variable declarations with code block scopes.
Register: it can only be used in variable declarations with code block scopes. It also prevents you from getting the address of the variable.
Static: Used to declare a variable with a code block scope, so that the variable has a static storage period, so that it can exist and keep its value while the program is running. When a variable is declared for the file scope, it indicates that the variable has an internal link.
Extern: indicates that you declare a variable that has been defined elsewhere.

12.3 storage classes and functions

Functions also have storage classes, which can be external or static. External functions can be called by functions in other files, while static functions can only be used in files that define them.

12.4 memory allocation: malloc () and free ()

Malloc (): it accepts a parameter: the number of memory bytes required, then malloc () finds a suitable block in available memory, and returns the address of the first byte of the block memory, if no space is found, a null pointer is returned.
Double * ptd;
Ptd = (double *) malloc (30 * sizeof (double ));
This code requests 30 spaces of double type values, and points ptd to the space lock location.
There are three ways to create an array:
1. Declare an array and use a constant expression to specify the array dimension.
2. Declare a variable-length array and use a variable expression to specify the array dimension.
3. Declare a pointer and call malloc ().
Generally, each malloc () call should be called once free (). The free () parameter of the function is the address previously returned by malloc (). You cannot use free () to release the memory allocated in other ways.
The header file stdlib. h contains the malloc () and free () prototypes.
The standard library provides two return values that ensure that the program works under all operating systems: EXIT_FAILURE indicates that the program is terminated abnormally, and EXIT_SUCCESS indicates that the program is terminated normally.

12.4.1 importance of free ()

During program compilation, the number of static variables is fixed and does not change during program running. The amount of memory used by the automatic variable increases or decreases automatically when the program is executed, but the amount of memory used by the allocated memory only increases, unless you remember to use free (). Otherwise, memory leakage may occur.

12.4.2 function calloc ()

Example:
Long * newmem;
Newmem = (long *) calloc (100, sizeof (long ));
The function calloc () has another feature: It sets all bits in the block to 0.
The function free () can also be used to release the memory allocated by calloc.

12.4.3 dynamic memory allocation and variable length Array

Both VLA and malloc () can be used to create an array of sizes determined at runtime.
The difference is that VLA is automatically stored, that is, the memory used by VLA is automatically released after the defined part is run.
Arrays created using malloc () do not have to be confined to a function.

Automatic variables: This part of memory is processed as a stack, which means that in the memory, new variables are added in order when they are created, and removed in reverse order when they are eliminated.

12.5 ansi c type limiters

C99 grants a new attribute to the type limiters: they are idempotent. This means that the same qualified word can be used more than once in a statement, and redundant words will be ignored.
Const int n = 6; equivalent to const int n = 6;
If the variable Declaration contains the const keyword, the value cannot be modified through the assign value, increment or subtract operator.

12.5.1 type qualifier volatile

The qualifier volatile tells the compiler that the variable can be changed by other proxies in addition to being changed by the program. Typically, it is used for hardware addresses and data shared with other parallel programs.
A value can be const and volatile at the same time. The program cannot change its value, which makes it const, but it is changed by a proxy other than the program, which makes it volatile.

12.5.1 type qualifier restrict

The keyword restrict can only be used as a pointer, indicating that the pointer is the only and initial method to access a data object.
Int ar [10];
Int * restrict restar = (int *) malloc (10 * sizeof (int ));
Int * par = ar;
The pointer restar is the only and initial way to access the memory allocated by malloc (), so it can use this keyword.
The par pointer is neither initial nor the only way to access data in the array ar, so it cannot be used.
Void * memcpy (void * restrict s1, void * restrict s2, size_t n );
Declaring s1 and s2 as restrict means that each pointer is the only way to access the corresponding data. Therefore, they cannot access the same data block, that is, they cannot overlap.

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.