Inline Function, macro definition, memory alignment, type conversion

Source: Internet
Author: User

Differences between macros and inline

Existing value, both of which are text replacement, reducing the number of program jumps and Improving Efficiency

1. Define is a pre-processing command and cannot be debugged. It can be replaced with the simplest text,

Inline is replaced during the compilation period. It can be debugged and exists.Parameter type check

2. When using inline, the function must be defined

Directly defined function, automatically converted to inline

 How does define transform class variables ..,

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

Memory alignment

Reason: Machine font length, addressing speed, computing speed, and platform transplantation reason

Principle: The starting address of each element is a certain multiple of sizeof (itself ).

Struct sets in struct. In principle, first alignment the inner struct and then alignment the external struct.

Union Max

Large-end short segment problem, TCP data packet conversion problem in Distributed Systems

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

Static_cast, dynamic_cast, const_cast, reinterpret_cast

Any problems can be solved by the compiler. Do not drag them to the runtime.

Upgrade 1

Char c = 123; int * P = (int *) & C; // Insecure code. The C compiler can run without making any judgment, but sometimes it may cause errors.

Strong conversion in C, replaced by static_cast <t> in C ++, performs security check during compilation. C: B, A; C is converted to a, or B, and the address is automatically calculated.

Upgrade 2

Sub-class parent class pointer conversion, static_cast <t> can do this, but it brings problems

If the subclass is converted to the parent class (large to small), the access may be out of bounds and an error is returned. This is a runtime error and cannot be processed by static_cast <t>.

As a result, dynamic_cast <t> appears, mainly used for downward conversion.

Dynamic_cast <source> (expression). The source and target must have an inheritance relationship, a virtual function, and a pointer and reference.

If it can be converted, it will be converted. If it cannot be converted, null will be returned (rtti, runtime type check)

 

Upgrade 3

Const_cast

In some cases, we cannot use the const variable. In this case, the const_cast attribute can be removed (only this time ).

 

Upgrade 4

Reinterpret_cast

Extremely dangerous, unaware of the value of existence

Inline Function, macro definition, memory alignment, type conversion

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.