가변인자

활용 예시

#include <stdio.h>
#include <stdarg.h>

void testit (int i, ...)
{
    va_list argptr; // 가변인자 리스트를 가리키는 포인터
    va_start(argptr, i); // 가변인자 리스트 포인터를 첫 주소로 초기화시켜준다.

    if (i == 0)
    {
        int n = va_arg(argptr, int);
				// int 타입 사이즈 만큼의 데이터를 반환하고, 포인터를 sizeof(int) 만큼 이동
        printf("%d\\n", n);
    }
    else
    {
        char *s = va_arg(argptr, char*);
				// char * 타입 사이즈 만큼의 데이터를 반환하고, 포인터를 sizeof(char *) 만큼 이동
        printf("%s\\n", s);
    }
	
    va_end(argptr); // argptr = NULL 로 가변인자 사용을 끝마침을 표시
}

코드 뜯어보기

stdarg.h에 포함된 함수와 타입의 정의는 다음과 같다.

type va_arg(
   va_list arg_ptr,
   type
);
void va_copy(
   va_list dest,
   va_list src
); // (ISO C99 and later)
void va_end(
   va_list arg_ptr
);
void va_start(
   va_list arg_ptr,
   prev_param
); // (ANSI C89 and later)

그리고 strarg.h는 다음과 같이 구현되어있다.

#ifndef _STDARG_H_
#define _STDARG_H_

/* All the headers include this file. */
#include <_mingw.h>

/*
 * Don't do any of this stuff for the resource compiler.
 */
#ifndef RC_INVOKED

/* 
 * I was told that Win NT likes this.
 */
#ifndef _VA_LIST_DEFINED
#define _VA_LIST_DEFINED
#endif

#ifndef	_VA_LIST
#define _VA_LIST
#if defined __GNUC__ && __GNUC__ >= 3
typedef __builtin_va_list va_list;
#else
typedef char* va_list;
#endif
#endif

/*
 * Amount of space required in an argument list (ie. the stack) for an
 * argument of type t.
 */
#define __va_argsiz(t)	\\
	(((sizeof(t) + sizeof(int) - 1) / sizeof(int)) * sizeof(int))

/*
 * Start variable argument list processing by setting AP to point to the
 * argument after pN.
 */
#ifdef	__GNUC__
/*
 * In GNU the stack is not necessarily arranged very neatly in order to
 * pack shorts and such into a smaller argument list. Fortunately a
 * neatly arranged version is available through the use of __builtin_next_arg.
 */
#define va_start(ap, pN)	\\
	((ap) = ((va_list) __builtin_next_arg(pN)))
#else
/*
 * For a simple minded compiler this should work (it works in GNU too for
 * vararg lists that don't follow shorts and such).
 */
#define va_start(ap, pN)	\\
	((ap) = ((va_list) (&pN) + __va_argsiz(pN)))
#endif

/*
 * End processing of variable argument list. In this case we do nothing.
 */
#define va_end(ap)	((void)0)

/*
 * Increment ap to the next argument in the list while returing a
 * pointer to what ap pointed to first, which is of type t.
 *
 * We cast to void* and then to t* because this avoids a warning about
 * increasing the alignment requirement.
 */

#define va_arg(ap, t)					\\
	 (((ap) = (ap) + __va_argsiz(t)),		\\
	  *((t*) (void*) ((ap) - __va_argsiz(t))))

#endif /* Not RC_INVOKED */

#endif /* not _STDARG_H_ */

가변인자들은 연속된 메모리 공간에 할당이 되어있다. 따라서 해당 가변인자를 활용하기 위해서 함수의 매개변수 중 ... 이전의 마지막 인자의 위치를 알아야한다. (*다른 매개변수들과 마찬가지로 함수가 실행되고 스택에 위치한다.)

Untitled

#if defined __GNUC__ && __GNUC__ >= 3
typedef __builtin_va_list va_list;
#else
typedef char* va_list;
#endif

현재 매개인자의 주소를 저장하는 타입