c++/c function

Source: Internet
Author: User
Tags define function

Three uses of the function stack:

1. Save the environment variable and return address before entering the function;

2. Save the copy of the argument when entering the function;

3. Save local variables in the function body.

Function call Specification: Define function argument stack, fallback stack and stack release mode.

1._CDECL: Function default specification, parameters from right to left stack, convenient variable parameter function, C + + static member function and friend function Use this specification.

2._thiscall:c++ the default specification for non-static member functions, you cannot use mutable parameters. When a non-static member function is called, the this pointer is saved directly in the ECX register, not the function stack.

Function Connection Specification:

1. Common C Connection specification:

#ifndef _cplusplus

extern "C" {

#endif

#ifndef _cplusplus

}

#endif

Function parameters:

1. C includes value passing and address passing, and C + + adds reference passing (the creation and destruction of references does not call the object's constructors and destructors).

2. The C parameterless function requires the use of void, otherwise it is considered to accept any type and number of arguments. The C + + parameterless function means that no arguments are accepted.

function return value:

1. c does not add functions that return value types, uniform is treated as int, and C + + does not allow no return value type.

2. The C + + return value is a reference, and if the return value is an internal local variable, it causes the reference to invalid memory.

3. Return value efficiency, such as:

1). Return String (a);

Temporary variables are created and initialized in an external storage area, eliminating the cost of copying and destruction.

2). String result (a);

return result;

The result object is created, invoking the constructor initialization. The copy constructor is then called, the result object is copied to the external store that holds the return value, and the destructor is destroyed at the end of the function to destroy the result object.

Storage type:

Global variables and global functions: extern

Global constants: Static

Local Variables: Auto

The register type is loaded into the CPU register, reducing the interaction overhead with the memory, typically a loop counter.

Connection type:

1. Outer joins: Called in other compilation units, such as global variables and global functions, or global constants that are modified by extern.

2. Internal connection: Only in a compilation unit is called, such as global constants, static modified variables, classes, anonymous federation, typedef definition.

3. No connection: a local variable or local class of a function.

c++/c function

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.