techiek7 techiek7 - 1 year ago 116
Linux Question

How much memory is allocated for argv[]?

I know command line arguments are character arrays and they are stored on stack. But I want to know actual memory allocated for of each argument. e.g. suppose I passed directory name "/tmp" as command line argument. This will be stored in argv[1]. But as I tested, it is allowed to change argv[1] to "/tmp/log/" (size increased) in program. How is this possible ?

Answer Source

To answer your question, the total maximum size available to argument strings and the passed environment can be obtained with:

getconf ARG_MAX

from the command line or the syconf equivalent from C (see for more information). (On my Linux box, the limit is 2097152).

Your example happens to work because the arguments and the environment are realistically stored contiguously, so appending to a string will overwrite what comes after it (following arguments, or the environment). And that's why it's a bad idea to try and expand the argv strings like that. If you want to modify them, either edit them or shrink them, but trying to expand them is a call for trouble.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download