Discuss the difference between the Register keyword in C and C + + _c language

Source: Internet
Author: User

In C + +:

(1) The Register keyword cannot define a variable in the global, or it will be prompted as an incorrect storage class.

(2) when the Register keyword is declared in a local scope, the address can be taken with the & operator, and once the address operator is used, the defined variable is forced to be stored in memory.

In C:

(1) The Register keyword can define variables in the global, and when the & operator is used on its variables, it simply warns of "Bad storage Class".

(2) The Register keyword can be declared in a local scope, but it cannot be used with the & operator. Otherwise the compilation does not pass.

It is not recommended to define a global variable with the Register keyword, because the lifecycle of a global variable begins at the beginning of the execution, until the end of the program, and the register variable may be stored in the CPU registers, if the register is occupied throughout the program's lifecycle, It's a pretty bad move.

The following are excerpted from the Internet from others:

A difference between C and C + + processing Register keywords

C + + is not fully compatible with the language, the last mentioned sizeof (' a ') equals a few of the problem is an example. Today, I stumbled across a different point in coding:

The variables decorated with the Register keyword can not be used in the C language to address the & operator, which is my experience. Because the compiler accepts the programmer's recommendation to save the variable to a register, it does not have a virtual address. But in C + +, the Register-modified variables can be addressed using the & operator, which I found in a piece of code. If the address of the register variable is explicitly taken in the program, the compiler will definitely define the variable in memory and not be defined as a register variable.

I have been confirmed in the C99 (ISO/IEC 9,899:1999) and ISO C + + (IEC 14,882:2003) standards, and C and C + + standards do have a distinct definition of what the register encounters with &. But why do you define this? I can only guess from the standard lines. How to describe the register in K&r C1 I have not verified, K&r C2 (ANSI C) describes the "register variables are to is placed in machine registers ... but Compi Lers are free to ignore the advice. But in C99 and ISO C + +, the wording goes: "suggests that access to the object is as fast as possible", "a hint to the implementation," O Bject so declared would be heavily used ", no special mention of" machine registers ". Visible historically register keywords in the emphasis as much as possible to save variables to registers, now the Register keyword no longer emphasizes specific means, but it is recommended that the compiler optimize access to the variable in a variety of ways (though many compilers ignore this keyword and adopt their own optimization strategy). C99 may be to maintain compatibility with k&r C and not allow address operations, and C + + perhaps because there is no historical baggage to relax the limit it. Guess only, the friend who wants to know the insider tells me more accurate answer.

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.