answersLogoWhite

0


Best Answer

There is nothing restricting you from doing so in the C language. However, that variable's memory will have been freed from the stack, and so it would be unsafe to modify that value or depend on the value, which will change the moment the stack overwrites the previous value. Only values stored in the heap may be safely returned by reference.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you return automatic variable by reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Difference between reference variable and instance variable?

An object is the actual storage space in memory in which some collection of data resides.A reference variable is a variable which refers to the memory location of an object.Look at the pseudocode below:Object obj = new Object();Here obj is the reference variable, and the data to which it refers is the object.


How do you return by reference in C plus plus?

Returning by reference implies that the reference will not fall from scope when the function returns (because references must never be NULL). Thus you cannot return a reference to a variable that was declared local to the function. int& foo() { int local; return(( int & ) local); // Error: cannot return a reference to a local variable. } However, you can return a reference to a parameter that was itself passed by reference, since that reference is guaranteed to exist (it can't be NULL). const int& GetMax( const int& byRef1, const int& byRef2 ) { return( byRef1 > byRef2 ? byRef1 : byRef2 ); // this is fine. } Generally, the only time you will return by reference is when returning a class instance member or a reference to the class instance itself, since they always remain in scope after the function call. class bar { public: bar():m_num(0){} const int& GetIntRef()const{return((const int&)m_num);} // constant reference to instance member. int& GetIntRef(){return((int&)m_num);} // non-constant reference to instance member. const bar& AsRef()const( return((const bar& )*this );} // constant reference to this instance. bar& AsRef()( return((bar&)*this );} // non-constant reference to this instance. private: int m_num; }; Note that use of const is not obligatory when returning references. However, when values must not be changed, it's good policy to enlist the help of the compiler wherever possible. The previous example returns both constant and non-constant references. The function that makes a call to GetIntRef() or AsRef() will determine whether the returned reference should be constant or not. By implementing both methods, we cater for both scenarios.


What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns. If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained. Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument. Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.


What type of reference is C19?

A reference variable is used to refer to or give access to an object. A reference variable is declared to be of a particular type and that type can not be altered.


What is reference data types in java?

A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed. Ex: ArrayList lst = new ArrayList(); The above line creates a reference variable lst which refers to an ArrayList object

Related questions

Why you use the reference variable?

The reference variable controls an object. Without the reference variable, you would have no way of accessing the object.


Difference between reference variable and instance variable?

An object is the actual storage space in memory in which some collection of data resides.A reference variable is a variable which refers to the memory location of an object.Look at the pseudocode below:Object obj = new Object();Here obj is the reference variable, and the data to which it refers is the object.


How to call a cell address in excel where the row number is a variable?

If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.If the row is variable but the column is fixed then it is a mixed reference. $A2 is a mixed reference. The row and column can be variable, in which case it is a relative reference. See the related question below.


How do you return by reference in C plus plus?

Returning by reference implies that the reference will not fall from scope when the function returns (because references must never be NULL). Thus you cannot return a reference to a variable that was declared local to the function. int& foo() { int local; return(( int & ) local); // Error: cannot return a reference to a local variable. } However, you can return a reference to a parameter that was itself passed by reference, since that reference is guaranteed to exist (it can't be NULL). const int& GetMax( const int& byRef1, const int& byRef2 ) { return( byRef1 > byRef2 ? byRef1 : byRef2 ); // this is fine. } Generally, the only time you will return by reference is when returning a class instance member or a reference to the class instance itself, since they always remain in scope after the function call. class bar { public: bar():m_num(0){} const int& GetIntRef()const{return((const int&)m_num);} // constant reference to instance member. int& GetIntRef(){return((int&)m_num);} // non-constant reference to instance member. const bar& AsRef()const( return((const bar& )*this );} // constant reference to this instance. bar& AsRef()( return((bar&)*this );} // non-constant reference to this instance. private: int m_num; }; Note that use of const is not obligatory when returning references. However, when values must not be changed, it's good policy to enlist the help of the compiler wherever possible. The previous example returns both constant and non-constant references. The function that makes a call to GetIntRef() or AsRef() will determine whether the returned reference should be constant or not. By implementing both methods, we cater for both scenarios.


How are the automatic variable also known as?

local variable


What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns. If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained. Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument. Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.


What are the relative advantages of using automatic and external variables?

An automatic variable is a temporary copy of a variable that is passed by value to a function. The only advantage to automatic variables is that any changes to the value are lost when the variable falls from scope when the function returns. However, the downside is it incurs a copy overhead, which can be excessive when the value is a complex structure or object. This is the same as passing a copy of an object by reference, except the copy remains in memory when the function returns. Passing by constant reference is always the preferred method of passing values to functions as this doesn't incur any copy overhead. If the variable must be passed as a non-constant reference, then you only need to pass a copy if you do not want the original object to be changed by the function. Automatic variables are also created with complex mathematical equations. It's always best to avoid these as much as possible, by storing the return values of the simpler equations. For example, int a = (5 * 12) / 3; This equation causes the (5 * 12) portion to create an automatic variable with the value 60. The following example produces the same result but doesn't require an automatic variable because the intermediate result is stored. int b = 5 * 12; b /= 3; The difference between these two is of little consequence, but in larger more complex equations the difference can soon mount up.


What type of reference is C19?

A reference variable is used to refer to or give access to an object. A reference variable is declared to be of a particular type and that type can not be altered.


What are the automatic variables in sas?

_N_ and _ERROR_ are the automatic variable in sas


What is the difference between value type parameters and reference type parameters?

When a variable is passed by value, the function receives a copy of the variable. When a variable is passed by reference, the function receives a reference, or pointer, to the original data.


What is reference variable and explain its uses?

Reference variable is an alias name for a previously defined variable. Its use is that the same data object can be referred by 2 names and then can be used interchangeably.


What is reference data types in java?

A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed. Ex: ArrayList lst = new ArrayList(); The above line creates a reference variable lst which refers to an ArrayList object