skip to Main Content

this is the code and it runs as intended.

    #include <stdio.h>
    int main( int argc, char* argv[])
    {
      printf (" this is the contents of argc:%dn",argc);
            
      int i;
      for (i = 0; i < argc ; i++){
        printf(" argv = %d = %sn",i,argv[i]);
      }
      return 0;
    }

When I change the argc in the for loop into a number, lets say 10, I still lesser than 10 the code crashes.

~/Desktop$ ./argc one two three
 this is the contents of argc:4
 argv = 0 = ./argc
 argv = 1 = one
 argv = 2 = two
 argv = 3 = three
 argv = 4 = (null)
 argv = 5 = SHELL=/bin/bash
 argv = 6 = SESSION_MANAGER=local/wajih:@/tmp/.ICE-unix/1230,unix/wajih:/tmp/.ICE-unix/1230
 argv = 7 = QT_ACCESSIBILITY=1
 argv = 8 = COLORTERM=truecolor
 argv = 9 = XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg

If I change argc in the for loop to a 100; this is the end of a very long error message

 argv = 54 = GDMSESSION=ubuntu
 argv = 55 = DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
 argv = 56 = LC_NUMERIC=ar_AE.UTF-8
 argv = 57 = _=./argc
 argv = 58 = OLDPWD=/home/wajih
 argv = 59 = (null)
Segmentation fault (core dumped).`your text`

I want to understand the reason this happens, thank you

I am new to c and programming in general, I was messing around to understand the concenpt of argc and argv.

4

Answers


  1. Most Unix System provides a 3rd argument to main function.

    int main( int argc, char *argv[], char *envp[]);
    

    It is called environment variables.
    In the above case it prints the contents of the 3rd argument – envp. But it will not show the same behavior always. Printing data from argv after argc count has undefined behavior

    Login or Signup to reply.
  2. You are invoking undefined behaviour. The C Standard says that argv[argc] will be a null pointer, and that trying to access argv[i] for i < 0 or i > argc is undefined behaviour.

    "Undefined behaviour" means anything can happen. If you ask for an explanation, there is none other than "it is undefined behaviour". It is legal for the compiler to produce code that completely erases your hard drive after sending all your money to my bank account. Don’t do it. You are doing things that you are not allowed to do, and that’s the complete answer.

    Login or Signup to reply.
  3. Going past the end of an array will give you undefined behavior in C. The results you would get would vary depending on the compiler, the operating system, the shell you use, and a lot of other factors.

    In this specific case, you are listing environment variables, because your main function is passed not just the arguments in argv but also a list of environment variables in envp, and just out of coincidence, those values are placed right after the argv array. Just remember that you can never trust that to be true.

    main(int argc, char *argv[], char *envp[]);
    

    In summary, don’t go past the end of the array. It will lead to Bad Things™.

    If your program needs to use the values of environment variables, you must to so through the envp array, and not abuse undefined behavior through the argv array.

    Login or Signup to reply.
  4. The answers above are true, but I think they just miss the point. The argv pointer has a very specific location in the program’s memory.

    When you run a binary, there is always some entry point. In C, that is in the main(...) function. But, in order to prepare the environment for the binary to start at that location, the OS has to do some things first. It has to copy over environment variables, request and offset memory from the OS, etc. Because this process is completely deterministic (per OS), you can actually expect to read the environment variables just after these arguments.
    Example memory layout on Linux

    This principle is fundamental to computer security. If an attacker manages to leak a pointer in this segment of memory, they can overwrite some environment variable (i.e. PATH), to point to their own binary first. hackmd has a really nice example of this.

    Good luck on your journey of learning C!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search