How much memory is allocated for argv[]?

<>

This question already has an answer here:

Answers


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 http://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html 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.


On Linux, parameters are populated by create_elf_tables. For this specific platform at least, you are correct that the values are stored on the stack.

Linux only uses exactly as much memory as is necessary to store arguments and (initial) environment variables on the stack; if you try to use more than what is already there, you're overwriting something else (or crashing).


The standard states that the argv can be modified since it is a special internal.

177 — The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination, so it is allocated only what you need at the assignment or replacement.

Standard text: http://c0x.coding-guidelines.com/5.1.2.2.1.html


Need Your Help

KeyListener and keyPressed

java swing user-interface

I'm trying to build screen keyboard and i need a little help.