In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is about how to analyze the implementation principle of variable parameter function. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
When learning the language of C, you must be exposed to standard output and standard input functions.
This function feels very powerful because it is very different, that is, the parameters of this function are variable.
The following is a self-written variable argument function, its function is summation. As follows
# include
# include
Int sum (int data,...)
{
Int iScripdataDiffect 0
Va_list vl
Va_start (vl,data)
While (iTunes music 1)
{
Sluggishi
I=va_arg (vl,int)
}
Va_end (vl)
Return s
}
Int main ()
{
Int s=sum (1, 2, 3, 4, 5, 5, 1)
Printf ("sum =% d\ n", s)
Return 0
}
As the program above, you can write functions with variable parameters by paying attention to the following points.
1. Statement
Int sum (int data,...)
It ends with. Ends, indicating a variable argument function.
two。 Correct use
Va_list
Va_start,va_arg,va_end
As above, you can write variable argument functions.
But most junior scholars may be right
Va_list
Va_start,va_arg,va_end
Hold back and think they are mysterious.
In fact, they are also very basic C knowledge, but are packaged.
The method of packaging in C, which has typedef,#define
For example, you package int as ID.
Typedef int ID
actually
Va_list
Va_start,va_arg,va_end
They are also packaged.
Va_list is a pointer type.
Va_start,va_arg,va_end, which is three macros.
Here is their source code in C #
As you can find in the header file stdarg.h, this article is for a higher version of the compiler for VC6.0. In vadefs.h, it is defined as
Something like that.
However, the key to this article is to talk about the principle of implementation:
First, when you call a variable argument function, for example
Above.
Int s=sum (1, 2, 3, 4, 5, 5, 1)
For this function, the compiler allocates space in memory to store these parameters.
Stack parameters according to the order from right to left in the compiler.
The stack here is often referred to as the memory space for storing local variables and function parameters.
This memory space is contiguous.
So all we have to do is find the first address of this memory space. Then each time a parameter is read, it is added that the parameter occupies the memory space.
Is the memory address of the next parameter, so you can get each parameter in turn.
The following is the meaning of each logo
A char* pointer is used to record the address of the parameter list in memory.
2dint vaults start ()
For example: va_start (vl,data); this macro gets the memory address of the second parameter in the parameter list.
As for why it's not the first one, it has something to do with the programmer who wrote the compiler, because its macro definition is to specify the vl, note (vl is va_list).
It points to the second parameter.
As above, v is the first parameter, and ap is the variable of va_list, such as the one above
Va_start (vl,data)
Obviously, it gets the memory address of the first parameter, and then adds the memory size of this parameter, which is the memory address of the next parameter.
Note: because the first parameter is already given in the parameter list, it gets the memory address of the second parameter.
3,va_arg
The meaning of this macro is to get the parameter referred to by the current vl, and the vl plus the size of the parameter points to the next parameter.
Its definition clearly illustrates this problem.
It first adds the size of the parameter type t, then subtracts the size of the parameter type t, and then does a cast (t *), so t has to be a class.
Type, not a variable. Then take * to get the data in the memory to which the address is changed.
For example, in the instance
I=va_arg (vl,int)
The first time it is called, it gets the value of the second parameter in the parameter list.
The values of the third and fourth parameters can be obtained by calling them in turn.
4,va_end
This macro is relatively simple.
Is to return the pointer value to 0. Make it point to NULL.
That is, if a pointer is not used, it will be assigned to NULL.
As above, you can see it first.
This macro is an assignment statement.
Ap= (char*) 0Placer vaults list is an alias for cha*.
In fact, as long as the four signs
Va_list
Va_start,va_arg,va_end
Once you remember what it means, you can write functions with variable arguments.
Main points for attention in use
Va_start (ap,v)
V is the first parameter.
Va_arg (ap,t)
T is the type of parameter you want to get.
Va_end (ap)
Is to assign the pointer ap to 0 so that it does not point to the variable in memory.
Ap
It sure is
A variable of va_list. That's one.
A variable of type char *.
At this point, I think you should have a certain understanding of the implementation principle of variable parameter functions, or at least a visual understanding.
Coder:huifeng00
The above is how to analyze the implementation principle of variable parameter function, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.