[C-prog-lang-l] traversing argv vs. environment variables

Walter Herold Veedla walterheroldv at gmail.com
Tue Apr 5 22:53:14 CEST 2022


Hello,

it was my code that ran from program arguments through the environment 
variables [and on...] until segfaulting.

 From the following link:
https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html
I was able to gather that "In Unix systems you can define main a third 
way, using three arguments: int main (int argc, char *argv[], char 
*envp[]) ... where the the third argument envp gives the program's 
environment."

This somehow implies that the program arguments and program variables 
happen to be laid out somewhat sequentially in memory and we run over 
them when we carelessly keep incrementing '++*argv' [and a whole lot 
more cryptic stuff]:).

The accepted answer in this stackoverflow post: 
https://stackoverflow.com/questions/18681078/in-which-memory-segment-command-line-arguments-get-stored
seems to confirm this.

Kind regards
Walter Herold Veedla

On 05.04.2022 22:15, Vladimír Kotal wrote:
> Hi all,
> 
> when writing the programs to print the argument vector using various 
> methods, someone raised a question in a Zoom chat that his program 
> actually printed environment variables (on a Unix box).
> 
> Could someone say what likely happened and why the environment variables 
> were printed ?
> 
> Best regards,
> 
> 
> V. Kotal
> 
> _______________________________________________
> c-prog-lang-l mailing list
> c-prog-lang-l at mff.cuni.cz
> http://mbox.ms.mff.cuni.cz/listserv/listinfo/c-prog-lang-l


More information about the c-prog-lang-l mailing list