函数参数最大数量

发布于 2024-12-29 08:27:06 字数 1389 浏览 0 评论 0原文

我在 C99 标准中没有发现 count 函数参数有任何限制,我猜它只是受堆栈大小的限制。

不过,我编写了一个简单的测试程序来演示具有大量参数的函数的行为。当大约 10k 时,我在 gcc 上出现以下错误(Cygwin 上的 gcc 版本 4.5.3):

/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../libcygwin.a(libcmain.o):(.text+0xa9): undefined reference to `_WinMain@16'

我意识到参数的数量不太可能如此之大,但我想知道编译器的哪个参数决定了这个限制?

编辑

脚本来生成C源代码

#!/bin/sh

num=$1

echo "" > out.c
echo "#include <stdio.h>" >> out.c

echo "int getsum( " >> out.c

i=0
while [ $i -lt $num ]
do
    ((i++))
    if [ $i -eq $num ] 
    then
        echo "int p$i )" >> out.c
    else 
        echo -ne "int p$i," >> out.c
    fi
done

echo "{" >> out.c

echo -ne "  return " >> out.c

i=0
while [ $i -lt $num ]
do
    ((i++))
        if [ $i -eq $num ]
        then
                echo "p$i;" >> out.c
        else
                echo -ne "p$i + " >> out.c
        fi
done

echo "}" >> out.c

echo "int main(){"  >> out.c
echo "printf(\"Sum of %d elements is %d\", $num, getsum(" >> out.c 

i=0
while [ $i -lt $num ]
do
        ((i++))
        if [ $i -eq $num ]
        then
                echo "$i" >> out.c
        else
                echo -ne "$i," >> out.c
        fi
done

echo "));" >> out.c

echo "return 0;}" >> out.c
gcc out.c
./a.exe

I did not find any limitation of count function parameters in the C99 standard and I guess it is only limited by stack size.

However I've written a simple test program to demonstrate the behavior of a function with a large count of parameters. When its about 10k, I get the following error on gcc (gcc version 4.5.3 on Cygwin):

/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../libcygwin.a(libcmain.o):(.text+0xa9): undefined reference to `_WinMain@16'

I realize that such large count of parameters is unlikely but I wonder what parameter of the compiler determines this limit?

EDIT

script to generate C-source

#!/bin/sh

num=$1

echo "" > out.c
echo "#include <stdio.h>" >> out.c

echo "int getsum( " >> out.c

i=0
while [ $i -lt $num ]
do
    ((i++))
    if [ $i -eq $num ] 
    then
        echo "int p$i )" >> out.c
    else 
        echo -ne "int p$i," >> out.c
    fi
done

echo "{" >> out.c

echo -ne "  return " >> out.c

i=0
while [ $i -lt $num ]
do
    ((i++))
        if [ $i -eq $num ]
        then
                echo "p$i;" >> out.c
        else
                echo -ne "p$i + " >> out.c
        fi
done

echo "}" >> out.c

echo "int main(){"  >> out.c
echo "printf(\"Sum of %d elements is %d\", $num, getsum(" >> out.c 

i=0
while [ $i -lt $num ]
do
        ((i++))
        if [ $i -eq $num ]
        then
                echo "$i" >> out.c
        else
                echo -ne "$i," >> out.c
        fi
done

echo "));" >> out.c

echo "return 0;}" >> out.c
gcc out.c
./a.exe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2025-01-05 08:27:06

该标准指定了每个实现必须支持的特定最小数量,

5.2.4.1 翻译限制

— 一个函数定义中有 127 个参数
— 一次函数调用中有 127 个参数

The Standard specifies a certain minimum number which every implementation must support,

5.2.4.1 Translation Limits

— 127 parameters in one function definition
— 127 arguments in one function call

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文