1. All functions are running in the Stack, which is called the active record of the function, which provides the storage area for each parameter in the active record, and the storage length of the parameters is Type Decision, parameter delivery is the process of initializing function parameter storage area using the real-den range of function calls. 1.1 Value Transfer of the default initialization method of the parameter transfer in C , which is copied in the storage area of the parameter (by value). When the value is passed, the function does not access the current call. The value of the function processed is its local copy, that is, the value of the activity record, so the change in these values does not affect the value of the argument. Once the function ends, the active record of the function is popped from the stack, and these local values disappear. The pointer transmission is essentially the value of the value, because the pointer variable itself is also a variable, and the system also creates a storage area for parameters of the active recording area as the pointer type, and initializes the storage area with the value of the real-guanxing variable, which is equivalent to the pointer. The value of the variable is incorporated into the function. If the pointer variable P is incorporated, the function stores the value of the variable in the active area, which is equivalent to creating a copy of the P, and the value of any change in the function in the function does not change the value of P, but because P and The initial value of _P is the same, and they all point to the value of other variables, so change the value pointed to by _P, which is equivalent to changing the value points to the P, but if the _p itself is changed, It does not affect the value of the P itself. Example: void test (int * p, char * p1) {* p = 1; Printf ("% x", & p1); Printf ("% x", p1); // output parameter P1 address, and P1 Value (also address) P1 = "Hello World";} void main () {char * p1 = "hello"; Printf ("% x", & p1); Printf ("% x", p1); // output variable The address of P1, also P1 value I = 0; int * p2 = & i; test (p2, p1); cout << p1 << "; << p2 << Endl;} The value of the output is output after execution : Hello; 1 (The output of the address is omitted, the address of the parameter P1 and the solid parameter P1 is different, but their values are the same). Why is a value changed, the other has no change. Since the statement P1 = "Hello World" is substantially changed in the function TEST, the value of the parameter P1 in the function activity record is altered, so that the objects of the reference P1 are different from the object points to the informators. So use the pointer to perform the function of the output parameter, you must use the pointer to the pointer, such as: void test (int * p, char ** p1) {* p = 1; * p1 = "hello world";} void main () {Char * p1 = "Hello"; int = 0; int * p2 = & i; test (p2, & p1); cout << p1 << "; << p2 << endl;} Output It is Hello World; 1. In this program, in the TEST function, the value of (* p1) points to the memory space is changed, that is, the value of the argument is changed. The value of the arguments is an address, so that the real parameters P1 point to another string.
There is also another thing to note that in the function body, it is a string constant, the memory space of the string constant in the constant data area, so it will not be released after the function is executed. If the program will make the reference to the function in the function body, the program will be wrong, because the stack space of the function is invalid after the function is executed, so the following code is wrong: void test (char ** p1) { Char a [10] = "Hello World"; p1 = a ;;} You can also create a heap space in the function body, let the real parameters P1 point to the heap space: void test (char ** p) {* p = new Char [10]; STRCPY (* p, "Hello World");} If these code is called, don't forget to release the memory of the stack space outside the function, if it is someone called, there is a memory leak. Danger, because others don't know how your code is written, he may only care about what is the string pointed to by the argument P1, it doesn't know if the space pointing in the function body is new, or the Malloc application, and there are also It may be that P1 points to a constant string (do not need to be released), so even if he wants to release these spaces correctly. Another solution is to use the reference to the pointer as a parameter, such as: void test (p, "hello world");} This will hand over the memory allocation to the caller, call The reference to the function of the memory pointer of the stack or stack, of course, this method has a problem, that is, the function itself does not know how big the memory space points to the parameter P, of course, the size of the memory space can be used as a size. The parameter incoming function, there is also a method to increase the allocated memory (cannot be redistributed in the function) (cannot be redistributed, otherwise the memory that the pointer to the point to leak). I don't know if there is a function that can calculate how big the memory pointed to by the pointer, or the memory pointed to by the pointer is a stack memory or a stack memory. If anyone knows, please tell me (email: 8280338@tzenet.com), make progress together :). Also mentioned, before using the parameter P in the STRCPY function and the sprintf function, P should have allocated space, not less than the space occupied by the string in the second parameter, if less than the second parameter Space, then it may not be reported when performing strcpy, but it will be reported when Delete P or Free (P) is reported. When the object is passed as a parameter, the meticulin is a copy of the argument, that is, after entering the function, the shape is constructed by the value of the arguments, so that the copy function of the class corresponding to the class is correct, in C The middle will automatically create four functions for each class: the default constructor, the destructor, copy function, and assignment functions. The automatically created copy function is a shallow copy. It is a bit copy. If a pile of memory is allocated in a class method, then the two pointers point to the same resource, and the same memory will be Double sectors, if such objects are passed to the function as parameters, the function in the actual parameters is not in the actual function. So this is a need for overloading the copy function.
It is actually determined whether the overloaded copy function is required, and it can be seen whether the destructor uses delete and free. If you use it, you will use the stack in the class method, you should overload the copy function, and if you overload the copy function Generally also reloaded assignment functions, the heavy-duty assignment function is simple, need to determine if it is self-assignment before calling the copy function, and then releases the own resources, then calls as long as the copy function can be called. When there is a pointer as a parameter transmission, it is necessary to use Assert (p! = Null) in the function body if necessary, if it is Malloc or New to apply for memory, IF (p! = Null) can be used to determine. 1.2 Another way to pass the delivery parameter is to reference the delivery, the reference delivery is to tell the function, directly access the actor, and create a storage area for the reference parameters in the activity record of the function: Void test (char * & p1) {COUT << "Para P: << & P << Endl; p1 =" Hello World ";} void main () {char * p1 =" hello "; cout <<" P: << & P << ENDL After test (P1);} After the program is executed, the address of the two P is the same, indicating that when the function is called, there is no storage space to the stack for the reference parameter. Using the reference parameters, the operations in the function body are directly for the arguments. In order to avoid errors, the const modifier can be used to write the operations in the function, such as: void test (const char * & p1) The communication of arrays is a pointer transmission, that is, pass the starting address of the array to the function, such as: void test (int TEMP []) {cout << sizeof (temp) << Endl; // output value is 4, That is, the space occupied by a pointer} 1.3 omitted number sometimes we cannot list all the types and numbers transmitted to the function, then we can use omitted 号 (...), suspend type checking mechanism. Such as: void test (param_list, ...); void test (...); the first declaration, the function call needs to be checked to type the explicit declared parameters, and the type check is hung for the university parameters, in this form The comma after the parameter declaration is optional, you can remove your comma. 1.4 Return Value of Function By default, the return value of the function is passed by value, so the function caller gets the copy of the RETURN return value in the function, and the original value and space will be released. Therefore, when the function returns a pointer and reference, it is necessary to pay attention to whether the pointer refers to whether the space is released, or if these problems are not present. Such as: vector
So if you need to return to the reference to the specified object, then the object should be built in the heap space, such as: Vector