C + + functions that support variable parameters

Source: Internet
Author: User
Tags function prototype

Why should I use a variable parameter function?

In general, when we are programming, the number of formal arguments in a function is usually determined, and all the actual parameters corresponding to the formal parameters are given in turn in the call. In some cases, however, the number of parameters you want to function can be determined as needed, so the C language introduces variable parameter functions. This is also a powerful aspect of C, some other languages, such as Fortran does not have this function.

Examples of typical variable parameter functions are familiar to printf (), scanf (), and so on.

B. C + + How to implement the function of variable parameters?

In order to support the variable parameter function, the C language introduces a new calling protocol, that is, the C language calling convention __cdecl. Use this calling convention by default when you are programming in C + + language. If you want to adopt other calling conventions, you must add other keyword declarations, such as the WIN32 API using Pascal calling convention, and the function name must precede the __stdcall keyword.

When the C calling convention is used, the parameters of the function are from right to left into the stack, with a variable number. Since the function body cannot know the number of incoming parameters in advance, the function caller must be responsible for stack cleanup when adopting this Convention. As an example:

//C调用约定函数
int __cdecl Add(int a, int b)
{
 return (a + b);
}

函数调用:
Add(1, 2);
//汇编代码是:
push        2            ;参数b入栈
push        1            ;参数a入栈
call        @Add         ;调用函数。其实还有编译器用于定位函数的表达式这里把它省略了
add         esp,8        ;调用者负责清栈

If the invocation protocol used when invoking the function and the inconsistency declared in the function prototype, the stack error is caused, this is another topic, which is no longer elaborate.

In addition, the C + + compiler supports variable parameter functions in the form of macros. These macros include Va_start, Va_arg, and Va_end. The reason for this is to increase the portability of the program. Masks the differences caused by different hardware platforms.

All macros that support variable parameter functions are defined in Stdarg.h and varargs.h. For example, in standard ANSI form, these macros are defined as:

typedef char * va_list; //字符串指针

#define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )

#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
#define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define va_end(ap)      ( ap = (va_list)0 )

Macro _intsizeof is used to align pointers by integer byte, because the parameter stack is an integer section (pointer or value) under the C calling protocol.

How to define a function of this kind.

Variable parameter functions are defined in different forms under different systems.

1. When using ANSI standard form, the prototype declaration of function with variable number of parameters is:

Type funcname (Type para1, type Para2, ...). );

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.