Another usage and danger of va (variable function parameters) in C Language

Source: Internet
Author: User

In C language variable parameter va (va_arg) We should be familiar with, mainly used to solve the problem of function parameter type and number uncertainty, basic usage and details please move here http://www.bkjia.com/kf/201202/119885.html
Generally, place va_arg on the right of the equal sign and extract its value for use:
[Cpp]
Func (Type para1, Type para2, Type para3 ,...)
{
/****** Step 1 ******/
Va_list ap;
Va_start (ap, para3); // you must specify the parameter before "...".

/****** Step 2 ******/
// At this time, the ap points to the first variable parameter.
// Call va_arg to obtain the value
Type xx = va_arg (ap, Type );

// The Type must be the same, for example:
// Char * p = va_arg (ap, char *);
// Int I = va_arg (ap, int );
 
// If multiple parameters continue to call va_arg
 
/****** Step 3 ******/
Va_end (ap); // For robust!
}
Another usage of va is introduced here: Use va_arg () as the left value:
[Cpp]
<Pre class = "cpp" name = "code"> // Function Definition:
PassInto (ID ,...)
{
 
Va_list valist;
 
Va_start (valist, ID );
 
* (Uint32_t *) va_arg (valist, uint32_t *) = 10;
Va_end (valist );
}
// Call:
Uint32_t value = 0;
PassInto (id, & value );
 
Printf ("% d", value );

The result is that the value is assigned to 10.
As you can see, va_arg is placed on the left of the equal sign. It seems very strange to use it. In fact, the principle of putting it on the right is the same. Because no matter how va works (see the above link ),
C-language functions are actually passed by values. In the first common usage, the va value is obtained and assigned to other variables. In the second usage, & value is passed as va,
Its value is actually a pointer to value, so va_arg (valist, uint32_t *) retrieves a uint32_t pointer, * (uint32_t *) va_arg (valist, uint32_t *) = 10 refers to converting the forced type of the pointer to (uint32_t *) and adding the * number for indirect reference,
In this way, the variable value is obtained, and now the value is 10, which is equivalent
[Cpp]
Value = 10;
In this way, all our operations are completed, and parameters of any type and number are assigned a value.
This method is extremely flexible, but do not perform the following dangerous operations:
[Cpp]
Uint16_t value = 0; // There is a change
PassInto (id, & value );
The consequences may be serious. Why? Because the parameter type does not match, the 16-bit parameter is forced to be used as 32 bits. At this time, our value team only applied for a 16-bit space from the system.
[Cpp]
* (Uint32_t *) va_arg (valist, uint32_t *) = 10
Put 10 to the 32-bit space starting from value. Where is the 16-bit space after value? Who is using it? Unknown.
If you are lucky, no one is using it, and the program has nothing to do. If the data is in use, the data will change, the system will be in use, and the system will also run away.
When I used it, it was not polite to me, and the system crashed.

From herbert's knowledge base

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.