argv[argc++] = bad_cast ("y");
This does not expand the argv array. It simply sets the value of argv[argc] , and then increments argc . This violates the original guarantee that argv[argc] == NULL , but so far the code does not rely on its validity.
The standard ensures that:
The parameters are argc and argv , and the lines pointed to by the argv array must be modified by the program and save their values stored at the last moment between the start of the program and the end of the program.
It does not explicitly guarantee that the char* pointers in the array pointed to by argv are mutable, but this is a reasonable assumption that they are. (Strictly speaking, argv points to the first element of the array, not the array itself, but this sentence has been around for quite some time.)
char** new_argv = realloc(argv, ++argc * sizeof*argv);
This behavior is undefined. The first realloc argument must be either a null pointer or a memory pointer allocated by malloc , calloc , realloc , or equivalent. The memory pointed to by argv is allocated in some vague way until main entered. You can make a copy of the array, but you cannot legally free it, which is part of what realloc does. If the realloc call behaved “right” for you, you're just out of luck. (If you were lucky, your program would crash, which would tell you that there is a problem.)
source share