C + + functions

Source: Internet
Author: User

I. Prohibit a function from returning a reference to a local variable

When a function call occurs, the compiler first puts the input/output parameters of the function onto the stack, the instruction register IP is placed on the stack (as the function returns the egress address), then the base address register, followed by the function's local variables. When a function returns, it performs a popup operation in the reverse order of the stack (first releasing the local storage variables in the stack, then the base register, the IP register address and the input/output variables of the function), and putting the IP register address on the stack as the exit address of the function and exiting the function. So all the local variables of the function are allocated on the stack, and the stack is freed when the function exits, and the memory that allocates the local variable is reclaimed by the operating system. The memory segment where the function local variable is located becomes exactly what the programmer cannot determine.

Therefore, when the function returns, it is guaranteed that the returned data is still valid after the function range, such as the introduction of the return local variable is not a reliable thing

When the function returns, it is also not a desirable method to return the object that is generated by new, because such code levels are confusing and will make the code upper user miserable

Two. function passing value. The efficiency analysis of transmitting pointer and transmitting reference

Value passing

char* getmemory (int nnumber)

{

char* pStr = new Char[nnumber];

return pStr;

}

int main ()

{

char* Phello = NULL;

Phello = getmemory (100);

strcpy_s (Phello, "Hello,world");

Delete[] Phello;

Phello = NULL;

}

The value-passing invocation process is divided into three steps:

1. Create parameter copies and local variables on the stack

2. Function execution

3. Function exits, releases the copy and temporary variables.

When a replica is generated, if the data type is a class type, the function invokes the class's constructor for class object construction and data copy, and if the data type is a class type when the stack releases the copy, the function invokes the class's destructor to release the class object data.

Reference delivery

Char getmemory (char* &pstr,int nnumber)

{

PSTR = new Char[nnumber];

return pStr;

}

int main ()

{

char* Phello = NULL;

GetMemory (phello,100);

strcpy_s (Phello, "Hello,world");

Delete[] Phello;

Phello = NULL;

}

A three-step reference to a value function

1. Create reference parameters, normal parameter copies and local variables on the stack

2. Function execution

3. Function exits, releases (references) copies and temporary variables.

The constructor and destructor calls do not occur during copy creation and release, even if the reference parameter is a class type during reference passing

Pointer calls 3 steps

1. Create pointer parameters, normal parameter copies and local variables on the stack

2. Function execution

3. Function exit, release copy and TEMP variable

A pointer copy, even if it is a class pointer, does not call the class's constructor because it is not a class object, but a class pointer

The dereference of a pointer is usually referred to as indirection, which is to get the data at the memory address in the pointer variable

Variance analysis

1. During the value transfer process, the formal parameters of the called function are treated as local variables of the modulated function, that is, the memory space is opened up in the stack to hold the value of the argument passed in by the main function, thus becoming a copy of the argument. Any operation of the modulated function on the formal parameter is performed as a local variable without affecting the value of the argument variable of the keynote function. This is not true if you want to exchange the two data by means of a value transfer.

2. During the reference transfer process, the formal parameters of the called function also open up memory space in the stack as a local variable, but this is where the address of the argument variable that is put in by the main key function is stored. Any operation of the modulated function on a formal parameter is handled as a pointer indirection, which accesses the argument variable in the central melody function through the address stored in the stack. Under

C + + functions

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.