When a function returns a value, a pointer, a reference, how is it taken out and received?

Source: Internet
Author: User
Tags constant

The first thing we need to know is that when you return a value, a pointer, a reference (less than eight bytes can be taken out of the register)

It is important to note that, for example, the address of a local variable is returned in the following example, which is risky and cannot be returned to a temporary local address or reference. A reference for thought analysis only

1. Returns the built-in type of the value, reference, pointer, respectively, with a pointer, value, reference to receive the case.

1) Returns a value from the Register

2) Returns a pointer through the register.

3) to return a reference, you need to dereference, return the pointer only need to return an address,

4) because the value returned by the register is not an address, the right side of the definition reference variable must have an address, and if you want to use a reference, you need to use a constant reference. You cannot accept the return value of this function with a pointer. This example is wrong.

5)
Returns an integer value that is received with a reference.
A constant reference generates a temporary amount after the function call completes.
The temporary amount is deposited at the dword[ebp-8] place.

6) A temporary amount is generated by referencing the address returned by a register by a constant pointer reference.

7) Returns a reference when the register is returned with an address, which is automatically dereferenced when the main function stack frame is returned. Then the reference is the variable that references the value. Value a desirable address.

Can also be written as int *p =&getintref ();
P points to the address of value.

2. Returns the value of the custom type, the reference, the pointer, respectively, with a pointer, value, reference to receive the case.

/**** A temporary amount is generated when a custom type returns a value. The return pointer does not produce a temporary amount, and when it returns a reference, it itself will dereference ****///Return the value of the custom type, using a pointer, value, reference to receive the case//custom a type DATA typedef struct _DATA {int A;}

DATA;
   Data GetData () {Data data={10};
return data;

  } int main () {///correct, return value returned by the Register DATA Ret1=getdata ();

   Correct, it should be noted here, although the register with the return, here is the value of the custom type, the reference when the compiler will automatically generate a temporary amount, and the built-in type can not be DATA &ret2=getdata (); VC does not point to the custom type returned (take the address first, the temporary amount in the back), and VS and GCC support pointers to the custom type register to bring out the return value (of course, VC is relatively old compiler, we have vs and gcc prevail, this is correct) DATA *ret3=&

   GetData ();
return 0; }//Returns a pointer to a custom type, using pointers, respectively, to receive the case of a typedef struct _DATA {int A;}

DATA;
   data* getdataptr () {static DATA data={10};
Return &data;

   } int main () {//Can DATA *ret1=&getdataptr ();
   No, when a pointer is returned, regardless of type, the pointer is related to the number of compiler bits, the pointer is always returned through the register, and the reference uses the oft-referenced DATA *&ret2=getdataptr ();
return 0; }//Returns a reference to the custom type, using pointers, respectively, to receive the case of a typedef struct _DATA {int A;}

DATA;
   data& getdataptr () {static DATA data={10};
return data; } int main () {///can, here refers to the value of the variable after the solution reference returned by the Register DATA &ret1=getdataptr();

   Can DATA *ret2=&getdataptr ();
   No, the right side of the equation here is a constant with a const reference to DATA *&ret3=&getdataptr ();
Const references DATA *const &ret3=&getdataptr () return 0;
  } int *& getintptr ()//error, can not return the reference to see if the return after the address {static int value=10;
  Return &value;//register to return an address} int *const& getintptr ()//correctly returns a constant reference {static int value=10;
Return &value;
  } int*& Getintptr ()//correct {static int value=10;
  static int *ptr=&value; return ptr;
   Preferable address} DATA *getdataptr () {static DATA data={10};
Return &data; } int main () {return 0;}

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.