为什么va_arg返回地址0,然后是1然后是2?

时间:2012-07-31 01:52:23

标签: objective-c ios variadic-functions

我正在检索va_args,但我不确定为什么它有时会返回0,1,2等等。当我尝试访问它时,它会崩溃我的程序,而我无法断言它返回的内容。

有什么想法吗?

背景:我在iOS编程。

void strngCls::Form(*wchar_t wCh, va_list argList)
{

    va_list argListTest = argList;
    const wchar_t *ws1 = va_arg(argListTest, const wchar_t *);
    const wchar_t *ws2 = va_arg(argListTest, const wchar_t *);

    if(ws1!=NULL && ws1!=(const wchar_t *)1 && ws2!=NULL)
    {
        int asd=wcslen(ws1);

        int asd2=wcslen(ws2);
        NSLog(@"ws1 = %d, ws2 = %d",asd,asd2);

        NSString *s1 = [[NSString alloc] initWithBytes:ws1 length:sizeof(wchar_t) *   wcslen(ws1) encoding: NSUTF32LittleEndianStringEncoding];
        NSLog(@"s1 %@",s1);
        NSString *s2 = [[NSString alloc] initWithBytes:ws2 length:sizeof(wchar_t) * wcslen(ws2) encoding: NSUTF32LittleEndianStringEncoding];
        NSLog(@"s2 %@",s2);


        NSString *iosString = [[NSString alloc] initWithFormat:@"%@ = %@", s1, s2];
        const char *ws3 = [iosString cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
        [s1 release];
        [s2 release];

        x = vswprintf(m_pchData, GetAllocLength(), (LPCTSTR)ws3, argListTest);

    }
    else if(ws1!=NULL && ws1!=(const wchar_t *)1)
    {
        int asd=wcslen(ws1);

        NSLog(@"ws1 = %d",asd);

        NSString *s1 = [[NSString alloc] initWithBytes:ws1 length:sizeof(wchar_t) * wcslen(ws1) encoding: NSUTF32LittleEndianStringEncoding];
        NSLog(@"s1 %@",s1);
        NSString *iosString = [[NSString alloc] initWithFormat:@"%@", s1];
        const char *ws3 = [iosString cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
        [s1 release];
    }

}

1 个答案:

答案 0 :(得分:1)

您错过了对va_start的调用:

void strngCls::Form(*wchar_t wCh, va_list argList)
{
    va_list argListTest = argList;
    va_start(argListTest, wCh);
    const wchar_t *ws1 = va_arg(argListTest, const wchar_t *);
    const wchar_t *ws2 = va_arg(argListTest, const wchar_t *);

此外,完成后请致电va_end

    va_end(argListTest);
}