Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why main(int argc, char** argv) allow to override argv parameters, why and how this work?

I found that main() allow override the argv[] parameters, as because they are not const.

#include <cstdio>

int main(int argc, char** argv)
{
    printf("%i %s\n",argc, argv[1]);
    argv[1][0] = 'X';
    argv[1][4] = 'X';
    printf("%i %s\n",argc, argv[1]);
    return 0;
}

and below is the result, it compiled and worked.
enter image description here

at first i expected some program crush like undefined behave however it may make sense that i may want to have a program that accept sensitive data as parameter but after i use it i would like to override it it immediately, is this is the only reason or there are others?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i would like to understand how this is work. i.e where are the char** argv[] strings array is located? how it created and deleted? etc.

>Solution :

On Linux/x86-64 the argc, argv, env is stored on the call stack by the kernel doing the execve(2) according to ABI conventions

See also this and the Linux Assembly HOWTO

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading