C + + Primer Plus reading notes-9th chapter memory models and namespaces

Source: Internet
Author: User
Tags constant definition function prototype translate function

9th chapter Memory model and namespaces

1. What the header file often contains:

Function prototypes.

Symbolic constants defined with # define or Const.

Structure declaration.

class declaration.

Template declaration.

inline functions.

2. If the file name is included in angle brackets, the C + + compiler looks in the file system of the host system where the standard header file is stored. However, if the header file name is enclosed in double quotation marks, the compiler will first look for the current working directory or source code directory (or other directory, depending on the compiler). If the header file is not found there, it will be looked up in the standard location. Therefore, when you include your own header file, you should use quotation marks instead of angle brackets.

3. The linker merges the target file code, library code, and startup code to generate the executable file.

4. In the same file, you can only include the same header file once. Therefore, it is commonly used:

#ifndef Coordin_h_

#define Coordin_h_

...

#endif

This protection does not prevent the compiler from containing the file two times, but simply letting it ignore everything except the first one.

5. The C + + standard runs each compiler designer to implement name decorations in a manner that he deems appropriate, so target code files created by different compilers are likely not to be properly linked. In other words, two compilers will generate different decorated names for the same function.

6. In the previous versions of C and C + + 11, the Auto keyword was used to default to automatic variables, so programmers rarely use it. In c++11, this usage is no longer legal. The Auto keyword in c++11 is automatic type inference. Standard-setting people are reluctant to introduce new keywords, because doing so may lead to illegal code for other purposes in the keyword. Given that the old usage of auto is seldom used, it is better to give new meanings than to introduce new keywords.

7. The keyword register was originally introduced by the C language, and it is recommended that the compiler use CPU registers to store the variable. In c++11, this effect is lost, and the keyword register simply indicates that the variable is automatic, which is exactly the same as the previous use of Auto. However, the important reason for retaining the register is to avoid illegal existing code that uses the keyword.

8. External links (can be accessed in other files)

Internal link (accessible only in the current file)

No link (accessible only in the current function or code block)

Create a static variable that is externally linked: Declare it outside the block of code;

Create a static variable that is internal to the link: Declare it outside the code block and use the static keyword;

Create a static variable that is not linked: You must declare it within a block of code and use the static keyword.

int global = 1000;//external link

static int one_file = 50;//internal link sex

void Funct1 (int n)

{

static int count = 0;//non-linked

}

9. If all static variables are not initialized, all bits are set to 0. Static variables are initialized with 0 initialization (default), constant initialization, and dynamic initialization.

10. Define the Declaration (definition), which allocates storage space for the variable.

A reference declaration (declaration) that does not allocate storage space to a variable because it refers to an existing variable.

The reference declaration uses the keyword extern and is not initialized and, if initialized, is defined, resulting in the allocation of storage space.

Double up;//definition Declaration

extern int blem;//Reference Declaration

extern char gr = ' z ';//definition declaration, because initialization

11. If a variable with the same name as an external variable is declared in the function, to refer to an external variable, precede the variable name with the following: The scope resolution operator, which represents the global version of the variable. Applies to C + + only.

P314 if a regular external variable is defined in a file, a regular external variable with the same name is defined in another file, which is a duplicate definition and will fail.

If a static external variable is defined in a file and a regular external variable with the same name is defined in another file, the static variable hides the regular external variable in the file.

13. If a static local variable is initialized, the program is initialized only once at startup. When the function is called later, it will not be initialized again as if it were an automatic variable.

Volatile prevents the compiler from being optimized.

15. Keyword mutable points out that even if a struct or class variable is const, one of its members can be modified.

struct data

{

Char name[30];

mutable int accesses;

};

Const data Veep = {"Claybourne Clodde", 0};

strcpy (Veep.name, "Joye Joux");//Not allowed

veep.accesses++; Allow

16. By default, global variables are linked externally, but the const global variables are internally linked (in C + +, not C language). That is, in C + + it appears that the global const definition is the same as using the static descriptor. This is why you can put a constant definition in a header file.

17. If for some reason the programmer wants a constant to be linked externally, the extern keyword can be used to override the default internal linkage: extern const int states = 50;

18. By default, the functionality is externally linked, and can be shared between files. In fact, you can use the keyword extern in a function prototype to indicate that the function is defined in another file, but this is optional (to have the program look for a function in another file that must be compiled as part of the program, or a library file that is searched by the linker). You can use the keyword static to set the linkage of a function to internal so that it can be used only in one file. You must use this keyword in both the prototype and the function definition:

static int private (double x);

...

static int private (double x)

{

...

}

As with variables, in a file that defines a static function, the static function overrides the external definition, so even if a function with the same name is defined externally, the file will still use a static function.

19. The single-definition rule applies to non-inline functions in addition to variables. This means that in a multi-file program, only one file can contain the definition of the function, but each file that uses the function must contain its function prototype. Inline functions are not constrained by this rule, which allows the programmer to place the definition of an inline function in a header file. In this way, each file containing the header file has the definition of an inline function.

20. Suppose a function is called in a file of a program, where does C + + go to find the definition of the function? If the function prototype in the file indicates that the function is static, the compiler will only look for the function definition in that file, otherwise the compiler (including the linker) will look in all files. If it is not found in the program file, the compiler will search for it in the library. This means that if you define a function with the same name as a library function, the compiler will use the programmer-defined version instead of the library function.

21. The linker requires that each different function have a different symbolic name. In C, a noun corresponds to only one function, so it is easy to implement. To satisfy internal needs, the C language compiler may translate function names such as Spiff into _spiff. This method is called C-language chaining (C language linkage). In C + +, however, the same name might correspond to multiple functions, and these functions must be translated into different symbolic names. Therefore, the C + + compiler performs a name correction or name decoration to generate different symbolic names for overloaded functions. For example, Spiff (int) is converted to-_spiff_i, and Spiff (double, double) is converted to _spiff_d_d. This approach is known as the C + + language's link (c + + language linkage).

What happens if you want to use a C-language precompiled function in a C + + program? For example, suppose you have the following code: Spiff (22); it has a symbolic name of _spiff in the C library file, but for our C + + linker, the C + + query convention is to find the symbol people call _spiff_i. To solve this problem, you can use a function prototype to indicate what convention to use:

extern "C" void Spiff (int);//using C-language chaining

extern void Spoff (int);//using the C + + language for linking

extern "C + +" void spaff (int);//using the C + + language for linking

The. New operator

If you want to allocate storage space and initialize the built-in scalar type (int, double), you can add the initial value after the type name and enclose it in parentheses.

int *pi = new int (6);

To initialize a regular structure or array, you need to use a list of curly braces to initialize, which requires the compiler to support C++11.

struct where{double x, double y, double z};

where *one = new where{2.5, 5.3, 7.2};

int *ar = new Int[4] {2, 4, 7, 6};

In C++11, you can also use the initialization list for single-value variables:

int *pin = new int {6};

When new fails

In the first 10 years, C + + had returned a null pointer when new failed, but now throws a Std::bad_alloc exception.

P321 positioning the new operator

The locate new operator is used to place information in a specific hardware address. Delete can only be used to release a block of memory allocated by the general new.

25. The namespace can be global or in another namespace, but not in a code block. Therefore, by default, the name declared in the namespace is externally linked (unless it references a constant).

C + + provides two mechanisms (using declarations and using-compilation directives) to simplify the use of names in namespaces. The using declaration makes a specific identifier available, and the using-compiler directive makes the entire namespace available.

The. Using declaration adds a specific name to the declaration area to which it belongs. When a using declaration is used outside the function, the name is added to the global namespace, and the name is added to the local declaration area when a using declaration is used inside the function (for example, if the name being added is a variable, it will be the same as the other local variables).

28. With regard to the using declaration and the using compiler directives, it is important to remember that they increase the likelihood of name collisions.

For example, if you use the scope resolution operator in your code, there will be no ambiguity:

Jack::p al = 3;

Jill::p al = 10;

However, if you use the using declaration, the situation will change:

Using Jack::p al;

Using Jill::p al;

PAL = 4;

The compiler does not allow the use of both of these using declarations at the same time, because this results in two semantics.

29. In general, using a use declaration is more secure than using a using-compiled directive because it imports only the specified name. If the name conflicts with a local variable, the compiler will issue an indication that the compilation does not pass. The using-compilation directives import all names, including names that may not be required. If it conflicts with a local name, the local name overrides the namespace version, and the compiler does not issue any warnings. In addition, the openness of namespaces means that namespaces may be scattered in multiple places, making it difficult to know exactly which names are added.

30. You can nest namespaces in namespaces, or you can use using compilation directives and using declarations in namespaces.

The using compilation directives are transitive. P330

32. You can create aliases for namespaces.

Namespace my_very_favorite_things{...};

namespace mvft = my_very_favorite_things;

You can use this technique to simplify the use of namespaces:

namespace MEF = Myth::elements::fire;

Using Mef::flame;

33. You can create unnamed namespaces by omitting the name of the namespace:

Namespace

int ice;

int bandycoot;

This is like following the using compiler directive, that is, the name declared in the namespace is potentially scoped to: from the declaration point to the end of the declaration area. From this perspective, they are similar to global variables. Because the namespace does not have a name, you cannot use the name in that namespace in a file other than the owning file. This provides a substitute for the static variables that are linked internally.

34. When importing a function name with a using declaration, because only the name of the function is given, if a function is overloaded, a using declaration imports all versions.

C + + Primer Plus reading notes-9th chapter memory models and namespaces

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.