Home > Back-end >  Discussion of C function return type for reference
Discussion of C function return type for reference

Time:04-02

Words when c + + function return type as the reference requirement in function parameters, contains a way by reference or pointer, need to be returned by the parameter ,
But why the following code will run normal compilation?

 
Int& F (int a, b int, int) c

{
C=a + b;
Return the c;
}

Int main () {

Cout & lt; & lt; F (1, 0) & lt; & lt; Endl;
}



Is my understanding of this sentence is wrong or which other words wrong?

CodePudding user response:

But sometimes, we will find that you even return variable references, when calling functions, you will also find that can return to the correct values, such as the function of the program, called in the main function, can return to the correct values, and the overthrow of the above conclusion, only that your program is too small, because the original store the address space of the local variable has not been applied by other code, is stored inside the value has not been covered, so would get the right result, if folding the address space is allocated to other code to use, that would be a mistake,
From the above code can also be found in the test results, is if the child () function returns a pointer, in the main function call, you will find that the return value and return temp address is the same, but the content of the stored into a random value

CodePudding user response:

Your that sentence is wrong understanding, the words mean, return values, function parameters is not the function return value, can act as a return value to make the function parameters, usually is a pointer or reference to function as a function parameter

CodePudding user response:

Your function retrieves a reference to a local variable, potentially dangerous,
As an array, can compile, of course, but the result is not necessarily correct or run to collapse,

CodePudding user response:

Have a winning lottery, but want to get rich can buy a lottery ticket?

CodePudding user response:

Compilation is no problem, but you returned to the local variables of reference, the end of this variable within f function is destroyed, if you continue to access this variable after it may cause crashes or error

CodePudding user response:

In addition, the application of no mistake to push out correctly, the most lucky

CodePudding user response:

Return type has nothing to do with function parameters, where did you see this requirement,
Compiled by no problem, do not represent runtime c is a local variable, should not return to a local variable references,

CodePudding user response:

The variable usually includes function arguments and function in the stack declared temporary variables,
The basic variables of the stack exit its scope, who did not perform a piece of code to release/destroy/destructor of memory, it occupies only is no one to stay at the top of the current stack of several legacy can be covered by subsequent pressure stack, useless data,
And the class variables in the stack exit its scope, will automatically perform its destructor,...

Actually computer after each byte of physical memory has values and is read/write, never because of the so-called new, delete or malloc, free and is created and destroyed, the difference only lies in the operating system memory management module can find when you read and write and whether to take appropriate action, and the granularity of the operating system manages memory rather than bytes page, a page is usually 4 KB,

CodePudding user response:

You can compile, but there are concerns, you can look at the same time, c function inside a function, store address

CodePudding user response:

Temporary amount of survival from it to the end of the complete expression,
In the design and evolution of c + + language is say so -

So your program can be run normally, it is in conformity with the specification,

CodePudding user response:

Return references to local variables have no grammatical errors, but the address is wild,
If an object instance is to avoid return big lead to performance loss, can be used in a c + + 11 rvalue references,
Return STD: : move (string)

CodePudding user response:

Local variable references, out of the scope, will not be recorded, but the basic types of data has not been cleared, address can also be used for reference, is likely to run after, will go wrong,
Is the so-called "can temporarily use" "wild pointer",


Please correct me a great god,

CodePudding user response:

refer to 12 floor x_xx_xxx_xxxx reply:
local variable references, out of the scope, will not be recorded, but the basic types of data has not been cleared, address can also be used for reference, is likely to run after, will go wrong,
Is the so-called "can temporarily use" "wild pointer",


Please correct me a great god,

Great god had already on correct 8 floor,
http://edu.csdn.net/course/detail/2344 C language pointer and assembly - a memory address. The code element

CodePudding user response:

The
reference 13 floor zhao4zhong1 reply:
great god had already on correct 8 floor,
http://edu.csdn.net/course/detail/2344 C language pointer and assembly - a memory address. The code elements


Ha, ha, ha, great god: hello!!!!!
Just watch links can't! Only members,

CodePudding user response:

On the 14th floor x_xx_xxx_xxxx
reference response:
Quote: refer to the 13th floor zhao4zhong1 response:

Great god had already on correct 8 floor,
http://edu.csdn.net/course/detail/2344 C language pointer and assembly - a memory address. The code elements


Ha, ha, ha, great god: hello!!!!!
Just watch links can't! Only members,

The "status quo" or to join as member, declare: I'm not CSDN childcare,

CodePudding user response:

If the input parameter is "(... Int & amp; C) "; Is safe right? Please correct me a great god

CodePudding user response:

Please refer to the "c + + functions return values (on)"
Wish I could help you!

CodePudding user response:

Although compile and run also didn't make a mistake, but this is the wrong wording, g + + will have warning
Demo2. CPP: In function 'int& F (int, int, int) ':
Demo2. CPP: saying: warning: the reference to a local variable 'c' returned [- Wreturn - local - addr]
Int& F (int a, b int, int) c

CodePudding user response:

An Error in the CB: [c + + Error] 2021 _1_25_unit1. CPP (10049) : E2363 Attempting to return a reference to a local variable 'c'


reference 16 floor Liuqibaa1 response:
if the input parameter is "(... Int & amp; C) "; Is safe right? Please correct me great god
experiment is as follows:
 int& F (int a, b int, int& C) 

{
Cout<& lt; C<& lt; Endl;//4
C=a + b;
Return the c;
}

Int main () {
Int c=4;
Cout & lt; & lt; F (1, 1, c) & lt; & lt; Endl;//2
Cout<& lt; C<& lt; Endl;//2
system("pause");
}

CodePudding user response:

This code doesn't change point
 # include & lt; Iostream> 
using namespace std;
# pragma warning (4996) disable:


Class A {
Char * _data while forming;
Public:
A (const char * s) : _data while forming (strdup (s)) {

}
A (const A& STR) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related