The origin of language, Wife in memory. (C language introduced by an array of individual learning recent summary)

Recent college life is like a firmly cycle, it has been in and turn that export condition seems to have been assigned to the 1, I'm a little south ~~~ ( tsu-ma ra nai! ). But more or less still a little harvest, like the self-energizing conditions, although tasteless, but wasteful to discard, would like water into the deep, there are dragon born Yan . (Mo nonsense to say, the words go and True Story).

After reading branching and looping structures, it came in contact with the more important functions and an array of these two. Meanwhile, after the teacher explained, I deeply aware of, not just utilize the programming language, object-depth understanding of the computer that is the proper way.

Bowl !

function

In the function part, the more important are calling mass participation and functions of these two functions.

Function parameter passing:

Function parameter passing There are two ways to pass parameters by value and pass-by-pass parameters. **

int exchange1(int a, int b)
{
	a = a^b;
	b = a^b;
	a = a^b;
	printf("exchange1内  :a=%d b=%d\n", a, b);
}
int exchange2( int*p,int*q)
{
	*p = *p^*q;
	*q = *p^*q;
	*p = *p^*q;
	printf("exchange2内  ;a=%d b=%d\n", *p, *q);
}

void main()
{
	int a = 3, b = 6;
	exchange1(a, b);
	printf("exchange1外  :a=%d b=%d\n", a, b);
	exchange2(&a, &b);
	printf("exchange2外  :a=%d b=%d\n", a, b);
	system("pause");
}

Wherein Exchange1 () function is passed parameter a, b two arguments. Exchange2 () function is a, b two arguments address. Cum transmission parameters respectively corresponding to two kinds of ways. During the function call, the results and the results in the called function in the main function we will print it out there at the results.
Here Insert Picture Description
Thus, in this operation needs two to achieve the number of pass-switched transmission parameters can change the value of the argument, parameter passing by value while changing the value argument. Noting a and b is completed in the called function within exchange1 exchange. (It went on to say).
This is the difference between mass participation by value and pass-by-pass parameters of the two methods. In practice, according to their operational needs or needs to choose which way to pass parameters to use in the end. The advantages and disadvantages of the following two ways.
Parameter passing by value ; disadvantages, not by changing the shape parameters to change the external arguments
advantages: the pass-parameter passing relatively defect.
Chuan Chuan site parameters (pointer ) (Note: At the time of the incoming address of the array, the array will dimensionality reduction problem has not yet learned where niche, so that the reader can hope to learn from other reference blog about the big brother ( with whom I you get to know you did not read the writing, might as well not write, so as not to mislead people ))
** ** advantages: you can change the external arguments by changing the parameter.
Defect : 1. poor readability.
2. The safety of the code is relatively low (the reference pointer is sure to pass judgment empty ----> NULL)
3. Reference may be shaped by external influences

Function call :

Function call There are two kinds of chained calls and nested calls. Which nested call has a special way of calling cum recursive call.

int max_(int a,int b)
{
	return a > b ? a : b;
}
void MAX(int a, int b,int c,int d)
{
	int x = max_(a, b);
	int y = max_(c, d);
	int z = max_(x, y);
	printf("MAX=%d\n", z);
}
void main()
{ 
	int a = 9, b = 6, c = 3, d = 12;
	printf("max_(a, max_(b, max_(c, d)))=%d\n", max_(a, max_(b, max_(c, d))));
	MAX(a, b, c, d);
    system("pause");
}

Wherein the main () function call two functions, namely max_ (), and MAX () function.
printf("max_(a, max_(b, max_(c, d)))=%d\n", max_(a, max_(b, max_(c, d))));
This paragraph is printed as part of the chain max_ function calls ( the return value of a function as a parameter to another function that is chained calls (perhaps a better word access with a chain)).

The MAX function calls max_ function. A function similar to this function using the B method is called nested calls. Nested loop a reason.
As for recursion. A function is to continue to call the function A. Only to arrive export conditions.
There are two recursive calls premise
1. The scale of the problem of shrinking, the shrink or the same solution. 2. be able to find the exit conditions. Indispensable. If the problem can not be reduced scale, or reduced after the solution change, then use recursion is meaningless. If there is no clear exit conditions, the function may have been recursion, causing the phenomenon of death recursive ( card explosions, computer heaven !).
Recursion is not a panacea, no recursion is totally unacceptable !
why? First of all, recursion is not a panacea , the recursive method actually reflects the "so", "repeat the same steps," the idea, it can use a simple program to solve some complex computational problems, but a large amount of computation . On the solution of such non-issues, usually using an iterative method, fast iterative methods, and less memory footprint, required less time (Why?). However, no recursion is totally unacceptable . The high on the Tower of Hanoi this repetitive, the same logic to solve the problem can be effectively solved using a recursive programmers think time, to reduce the time of writing the code. ( I can write a few lines I definitely do not write hundreds of lines 233 ).

For reasons of space and time consuming.

Each time a function call, we need to open up space on the stack memory space to execute the called function and temporary variables. Upon executing the transfer function of the back, where the spaces not released, while in like temporary variables within the temporary data space will be released ( ab-exchange is not complete hereinbefore max_ () function and the function ab exchange successfully completed execution reason ).
At the same time every time to open up space and free space, there will be time-consuming. So the function call is a time cost and space cost.

Other non-recursive function calls in the call. Once back transfer function executed, freeing up space to complete the call. Even if it is nested multiple calls will not consume too much time and space costs. But when a recursive call if there are problems larger, smaller or downsizing, you need to be near the exit condition by increasing the number of recursion.
Often more than one, once the space-time costs add up. It will be very prominent, resulting in significant delay and Caton.

After first-out principle stack area (Caishuxueqian, personal view at this stage)

Mentioned above, it will open up space on the stack memory space in the function call. So on the stack recursion is how to operate it?
Here Insert Picture Description
Because the function is called first, finished running and then release. And this figure recursive return after the return statement in the last part has called, so, at the first call, will first perform text (a-1) When you run this function return, in order to achieve the function call. But this time the text (a) function does not run to completion, his space there, and so once the same token, only when the last function both text (1) is finished, return return1 again after entering the text (2) This when, text (2) considered finished, return a value up to free up space again, after the completion of text (3) is considered finished running, repeat the previous implementation of the process online, only when all the calls all finished, the original call text (a) to complete. Both the first call recursion is first produced, and also is the latest to disappear. We called these two processes pushing and popping. From the time perspective, the recursive point of view is from the stack - the first after the stack by the stack after stack, first out stack.
(I am not very in-depth study yet, there may be errors, if useful, natural is best, if not useless, would like to smile a smile)

Array

If staying here to start the promotion of multi-dimensional array from one-dimensional array.
Is well known, the array is a combination of elements having the same data type. Define and initialize the array, now open up within a contiguous memory space from the space, according to the data type and the carrier be divided. This shows that when memory address one-dimensional array of continuous.

Here Insert Picture Description
as the picture shows.
So when the two-dimensional array address what happens then.
Here Insert Picture Description
Then the three-dimensional array do
Here Insert Picture Description
so, multidimensional arrays in the memory space is continuous.
Second, another look, you will find all the addresses are also increasing.
Therefore concluded that all of the array, whether it is a few dimensions, in memory of his address every element is continuous and increasing (** continuous not completely continuous, but a difference of a data type byte size **)
derived again, a two-dimensional array can be seen as an element of an array of one-dimensional array, a three-dimensional array is an array of two-dimensional array elements, the four-digit group of elements is three-dimensional array of one-dimensional array both n-dimensional array of elements may be viewed as a one-dimensional array of n-1 dimensional array.
They are characterized by having the above conclusion, both the array element in memory space is continuous and incremental.

Learning progress almost on here, take the time summed up the weekend, there may be wrong with the hope of being bigwigs correction. Personal feeling is, learning computer, or deep step further thought to be the largest harvest, to think about why. Knowledge of language tools, get to know the principles and clarify the nature. It will be able to adapt more quickly after the upgrading of knowledge. After all, he was just a computer computer, but the computer after all, he was a computer.
As shown signs of life not only willing !

Published 13 original articles · won praise 13 · views 756

Guess you like

Origin blog.csdn.net/jiewaikexue/article/details/102770358