7

It will look like int main(int argc, char *argv[]);. My questions are:

1 How many array items can I add in argv[]?

2 What is MAX size of every char *?

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
javas
  • 73
  • 1
  • 3
  • 1
    I think it's implementation-dependent, you cannot rely on any specific bound. – Vlad Sep 21 '11 at 11:26
  • 1
    If you need to pass so many parameters that you need to think about these upper bounds, your might be doing something wrong. You probably need some form of IPC. – Tamás Szelei Sep 21 '11 at 11:26
  • 1
    But _why_ do you need this information? If you need, perhaps you are doing something wrong. – Vlad Sep 21 '11 at 11:27
  • 1 I use linux 2 I will pass a lot info to it by PHP script using exec(). 3 I think use file to exchange the data will cost more time than using arguments of main function. thanks – javas Sep 21 '11 at 11:36
  • @javas: a file is not the only form of IPC, for example you could use `popen` and write a lot of data through the pipe. But on linux, command lines are allowed to be very long anyway, to support things like `ls *log`. – Steve Jessop Sep 21 '11 at 11:39

4 Answers4

4

You can try:

$ getconf ARG_MAX
2180000

http://pubs.opengroup.org/onlinepubs/007904975/basedefs/limits.h.html

ARG_MAX is maximum length of argument to the exec functions including environment data.

That is, there is no individual limit on the number of arguments or argument's length. Only the limit on total size required to store all the arguments and environment variables.

xargs figures out maximum command line length using sysconf(_SC_ARG_MAX); which yields the same value as reported by getconf ARG_MAX.

On Linux command line arguments and environment variables are put into new process' stack. So, the process/thread maximum stack size is the ultimate upper bound. Linux-specific limits are hardcoded in the kernel:

#define MAX_ARG_STRLEN (PAGE_SIZE * 32)
#define MAX_ARG_STRINGS 0x7FFFFFFF
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
3

Both of those are bounded only by how much memory you have (or how much memory your OS gives your program).

EDIT: Actually, the number of arguments is also bounded by the size of int.

flight
  • 7,162
  • 4
  • 24
  • 31
  • 2
    Or how much memory your OS gives for the cmd line (which is quite limited in most OSs out there, and sometimes very hardcoded) – PlasmaHH Sep 21 '11 at 11:29
  • I think this answer is answering "what is the max size of any char* ever", whereas the questioner, probably due to a misunderstanding, is asking what the maximum size of the `char*`s in argv[] are. – Jon Bright Sep 21 '11 at 11:32
  • Windows gives you very little memory (sometimes it chokes on just a few kB of command line args), while all Unix platforms are so generous that if you need more, you're doing it wrong. – Nicholas Wilson Sep 21 '11 at 11:43
0

I think you're misunderstanding what's going on here. You don't, in your code, add anything to argv[], and you don't worry about their maximum sizes. When somebody runs your compiled program, as

./javas_program argument1 argument2 argument3

then your main function will be called. argc will be 4, argv[0] will be ./javas_program, argv[1] will be argument1, argv[2] will be argument2, etc.

In your program, you should assume that the contents of argv[] can be any size. If you want to limit them to a specific size, you should check that they're not larger than that.

Jon Bright
  • 13,388
  • 3
  • 31
  • 46
0

It probably depends on the mechanism you are using to start your program. If it is through a shell (bash or whatever) you'd have to look up if it imposes restrictions.

If you start your program through execv or something similar, they should only be subject to the same restrictions as any array and string, and, as somebody noted, because argc is int for historical reasons, to the limited size of int not of size_t.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177