c - 函数参数最大个数

标签 c parameter-passing stack-overflow

我没有发现 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

最佳答案

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

5.2.4.1 翻译限制

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

关于c - 函数参数最大个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9034787/

相关文章:

ruby-on-rails - Rails - 更新未选中的复选框时缺少参数(作为 nil 值)

c - 在旧的原始套接字教程中使用 ntop() 而不是 ntoa()

c - 如何在链表中向后移动?

c - 为什么调用 bsearch() 会使呈现的程序崩溃?

c - 堆栈溢出和段错误

java - 给定算法分析的"main"java.lang.StackOverflowError

java - Stack OverFlow 与继承的 JPanels

c - 更快的算法来找出有多少数字不能被一组给定的数字整除

android - Activity 作为参数

python - 函数的返回值作为类属性