如果你不遵循argv和argc的实践,会发生什么?

时间:2021-09-23 21:15:05

Possible Duplicates:
main(int argc, char *argv[])
Main's Signature in C++

可能的副本:main(int argc, char *argv[])在c++中的签名

If i write:

如果我写:

int main(int argc, char** argv)

i get proper commandline input. What would happen if i wrote say,

我得到了正确的命令行输入。如果我写道,

int main(int foo, double fooDouble, fooType FooVar)

Is this OS-dependent or does it depend on the compiler?

它是依赖于操作系统还是依赖于编译器?

4 个解决方案

#1


6  

Given that it does compile, it will still only be called with the argc and argv arguments.

如果它确实进行了编译,那么仍然只能使用argc和argv参数调用它。

So your fooDouble will get the pointer value of argv, and FooVar will get whatever value is in that register/stack space used for that argument position (which may not have been initialized by the callee, so it may hold any undefined value).

因此,fooDouble将获得argv的指针值,FooVar将获得用于该参数位置的寄存器/堆栈空间中的任何值(可能没有被callee初始化,因此它可能包含任何未定义的值)。

#2


5  

This code doesn't even have to compile. If it does, undefined behaviour may occur.

这段代码甚至不需要编译。如果是这样,可能会出现未定义的行为。

#3


5  

The effect of adding a third (or fourth, or fifth...) argument to main indeed depends on the operating system. For instance, on Windows (and I believe on Unix as well) you can have a third argument which grants access to the environment variables:

向main添加第三个(或第四个,或第5个…)参数的效果实际上取决于操作系统。例如,在Windows(我也相信Unix)上,您可以有第三个参数,授予对环境变量的访问权:

int main( int argc, char **argv, char **env )

#4


2  

The C standard (BS ISO/IEC 9899:1999) gives two alternatives for main:

C标准(BS ISO/IEC 9899:1999)为main提供了两种选择:

int main(void) { /* ... */ }

and

int main(int argc, char *argv[]) { /* ... */ }

It also allows equivalents, so an argv of char ** argv for example. Additional arguments are "neither blessed nor forbidden by the Standard". Such addtions will be compiler and runtime (not operating system) specific.

它也允许等价物,例如char * argv。附加的论点“既不受标准的认可也不被禁止”。这些添加将是特定于编译器和运行时(而不是操作系统)的。

The arguments are passed by the C runtime, which calls main(). Passing any other type would be problematic on general environments like Windows and UNIX, so quite how you would expect to pass a double or fooType is beyond me.

参数由调用main()的C运行时传递。在Windows和UNIX这样的通用环境中,传递任何其他类型都是有问题的,所以我无法理解您如何传递double或fooType。

Invoking a program from either a command-line or using interfaces like execve (UNIX) or CreateProcess (Win32) involves passing zero delimited strings. In theory you could hack it to pass a binary value and then cast it in main, provided it does not contain a '\0' anywhere except at the end, which would be challenging.

从命令行或使用execve (UNIX)或CreateProcess (Win32)等接口调用程序需要传递零分隔字符串。理论上,您可以将它修改为传递一个二进制值,然后将其转换为main,前提是它不包含“\0”,除非在末尾,否则这样做是很有挑战性的。

EDIT: it occurs to me that you can call main() from within your program - the well known obvuscated C code "The twelve days of Christmas" does this. In this case there is no reason why you can pass anything the prototype allows.

编辑:在我看来,你可以从你的程序中调用main(),这是众所周知的“圣诞节的12天”。在这种情况下,您没有理由可以传递原型允许的任何内容。

#1


6  

Given that it does compile, it will still only be called with the argc and argv arguments.

如果它确实进行了编译,那么仍然只能使用argc和argv参数调用它。

So your fooDouble will get the pointer value of argv, and FooVar will get whatever value is in that register/stack space used for that argument position (which may not have been initialized by the callee, so it may hold any undefined value).

因此,fooDouble将获得argv的指针值,FooVar将获得用于该参数位置的寄存器/堆栈空间中的任何值(可能没有被callee初始化,因此它可能包含任何未定义的值)。

#2


5  

This code doesn't even have to compile. If it does, undefined behaviour may occur.

这段代码甚至不需要编译。如果是这样,可能会出现未定义的行为。

#3


5  

The effect of adding a third (or fourth, or fifth...) argument to main indeed depends on the operating system. For instance, on Windows (and I believe on Unix as well) you can have a third argument which grants access to the environment variables:

向main添加第三个(或第四个,或第5个…)参数的效果实际上取决于操作系统。例如,在Windows(我也相信Unix)上,您可以有第三个参数,授予对环境变量的访问权:

int main( int argc, char **argv, char **env )

#4


2  

The C standard (BS ISO/IEC 9899:1999) gives two alternatives for main:

C标准(BS ISO/IEC 9899:1999)为main提供了两种选择:

int main(void) { /* ... */ }

and

int main(int argc, char *argv[]) { /* ... */ }

It also allows equivalents, so an argv of char ** argv for example. Additional arguments are "neither blessed nor forbidden by the Standard". Such addtions will be compiler and runtime (not operating system) specific.

它也允许等价物,例如char * argv。附加的论点“既不受标准的认可也不被禁止”。这些添加将是特定于编译器和运行时(而不是操作系统)的。

The arguments are passed by the C runtime, which calls main(). Passing any other type would be problematic on general environments like Windows and UNIX, so quite how you would expect to pass a double or fooType is beyond me.

参数由调用main()的C运行时传递。在Windows和UNIX这样的通用环境中,传递任何其他类型都是有问题的,所以我无法理解您如何传递double或fooType。

Invoking a program from either a command-line or using interfaces like execve (UNIX) or CreateProcess (Win32) involves passing zero delimited strings. In theory you could hack it to pass a binary value and then cast it in main, provided it does not contain a '\0' anywhere except at the end, which would be challenging.

从命令行或使用execve (UNIX)或CreateProcess (Win32)等接口调用程序需要传递零分隔字符串。理论上,您可以将它修改为传递一个二进制值,然后将其转换为main,前提是它不包含“\0”,除非在末尾,否则这样做是很有挑战性的。

EDIT: it occurs to me that you can call main() from within your program - the well known obvuscated C code "The twelve days of Christmas" does this. In this case there is no reason why you can pass anything the prototype allows.

编辑:在我看来,你可以从你的程序中调用main(),这是众所周知的“圣诞节的12天”。在这种情况下,您没有理由可以传递原型允许的任何内容。