简体   繁体   English

为什么这段代码报告 -31 大于 6?

[英]Why does this code report that -31 is greater than 6?

I have a function double max(int count, ...) in my program.我的程序中有一个函数double max(int count, ...) This function should return the highest number, but it reports that -31 > 6 .这个函数应该返回最大的数字,但它报告-31 > 6 Where is my mistake?我的错误在哪里? I'm trying to learn va_ .我正在努力学习va_ How can I fix this?我怎样才能解决这个问题?

double max(int count, ...)
{
    double max = INT_MIN, test;

    int i;
    va_list values;
    va_start(values, count);
    for (i = 0; i < count; ++i)
    {
        test = va_arg(values, double);
        if (test > max)
        {
            max = test;
        }
    }
    va_end(values);
    return max;
}

int main()
{
    printf("%ld", max(5, 1, 6, -31, 23, 24));
    return 0;
}

You are invoking undefined behavior .您正在调用未定义的行为 You are not passing in double values to max() , you are passing in int values instead.您没有将double值传递给max() ,而是在传递int值。 int and double are different sizes in memory, and va_arg() can't read an int parameter as if it were a double , and vice versa. intdouble在内存中的大小不同,并且va_arg()无法读取int参数,就好像它是double一样,反之亦然。 You need to match the types correctly.您需要正确匹配类型。

In this example, change all of the double s to int s, eg:在此示例中,将所有double s 更改为int s,例如:

int max(int count, ...)
{
    int max = INT_MIN, test;

    va_list values;
    va_start(values, count);
    for(int i = 0; i < count; ++i)
    {
        test = va_arg(values, int);
        if(test > max)
        {
            max = test;
        }
    }
    va_end(values);
    return max;
}

int main()
{
    printf("%d", max(5,1,6,-31,23,24));
    return 0;
}

Online Demo在线演示

Otherwise, change the int s to double s instead, eg:否则,将int s 改为double s,例如:

double max(int count, ...)
{
    double max = -DBL_MAX, test;

    va_list values;
    va_start(values, count);
    for(int i = 0; i < count; ++i)
    {
        test = va_arg(values, double);
        if(test > max)
        {
            max = test;
        }
    }
    va_end(values);
    return max;
}

int main()
{
    printf("%lf", max(5,1.0,6.0,-31.0,23.0,24.0));
    return 0;
}

Online Demo在线演示


Either way, you might consider a slight change in logic in how you are handling the max value.无论哪种方式,您都可以考虑在处理max的逻辑上稍作改变。 You should initialize it to the 1st value that is passed in, eg:您应该将其初始化为传入的第一个值,例如:

<type> max(int count, ...)
{
    if (count <= 0)
        return <default value>;

    va_list values;
    va_start(values, count);

    <type> max = va_arg(values, <type>), test;

    for(int i = 1; i < count; ++i)
    {
        test = va_arg(values, <type>);
        if (test > max)
        {
            max = test;
        }
    }

    va_end(values);
    return max;
}

Also, note that all of the above is the C way of handling things.另外,请注意,以上所有内容都是 C 处理事物的方式。 But you also tagged your question as C++, and the C++ way to handle this would be to use a variadic template instead of ellipses (ie, no va_arg() needed), or else take a std::initializer_list or at least a pair of iterator s so that you can then make use of the standard std::max_element() algorithm.但是您还将您的问题标记为 C++,处理此问题的 C++ 方法是使用可变参数模板而不是省略号(即不需要va_arg() ),或者采用std::initializer_list或至少一对iterator s,以便您可以使用标准的std::max_element()算法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 std::sort 在比较函数使用大于 (&gt;) 而不是大于或等于 (&gt;=) 时起作用? - Why does std::sort work when the comparison function uses greater-than (>), but not greater-than-or-equal (>=)? 在这段python代码中,大于号的含义是什么? - What does the greater-than symbol mean in this piece of python code? 为什么此代码打印0到297,而不打印0到大于297的东西? - Why this code is printing 0 to 297 but not 0 to anything greater than 297? 为什么这段代码只识别文本文件中的第一行? 输入任何大于 1 的数字将返回“未找到原子数” - Why does this code only recognize the first line in the text file? Inputting any number greater than 1 returns "Atomic Number Not Found" 为什么我的代码不适用于大于 N=508 的值? - Why My Code Is Not Working For Values Greater Than N=508? c++:为什么我的 boolean 存储的值大于 1? - c++ : Why does my boolean store a value greater than 1? 为什么clang中的-O2或更高优化会破坏这段代码? - Why does -O2 or greater optimization in clang break this code? 为什么我的“小于”算法返回“大于”值? - Why does my 'less than' algorithm return 'greater than' values instead? 为什么我的递归代码以递增顺序打印 no 不适用于大于 10^5 的输入? - why my recursion code to print no in increasing order not working for input greater than 10^5? 当我使用大于1.0f的颜色值时,为什么OpenGL会这样做? - Why does OpenGL act this way when I use color values greater than 1.0f?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM