0

Accoding to this link, we need to terminate the list of argumnets by a null pointer. This is stated as :

The list of arguments must be terminated by a null pointer.

My question is why we need to terminate the list of argument by a null pointer ? Is this used because to know the end of the list of argument, which is an array to char pointers ?

8
  • you're assumption seems correct Commented Mar 14, 2023 at 20:59
  • Yes. Also, where the function declaration calls for environment arguments (envp list), that list too must be terminated by another NULL pointer, even when the envp list is otherwise empty. All these Section 3 functions are a thin layer that maps onto the execve() call, which is a Section 2 (system call) action, and has a rather better and more complete description of the interface. Commented Mar 14, 2023 at 21:55
  • Agreeing with my two previous commenters; this is done to know the end of the list. There's an interesting historical question there, which is why they chose to go the less space efficient, less safe way of zero termination instead of adding another length parameter, but alas, one can assume they must have expected programmers to be aware of C string pitfalls, so that this was at least consistent. Commented Mar 15, 2023 at 7:02
  • 1
    @MarcusMüller Notably, the argv list passed through execve() eventually gets placed on the new process's stack, and the call to main() has magically gained an argc. I always wondered why there is no envc. Seems to me that before getenv() existed, argc was just an optimization to enable an envp name search to know where envp[0] was. Commented Mar 15, 2023 at 9:38
  • @Paul_Pedant yeah, I always wondered the converse: Since there's an argc, why bother having the terminating zero pointer in argv? People talking to me about the strength and weaknesses of the C language and its explicit and cultural descendants know that I'm not at all, even remotely, a friend of the C string. It's been a bad idea in 1969, and it's still one of the, if not the, most common reason for software buffer bugs. It solves a problem nobody actually cares about, with a solution that's bad for software quality and speed. Commented Mar 15, 2023 at 10:45

2 Answers 2

1

why we need to terminate the list of argument by a null pointer? Is this used because to know the end of the list of argument, which is an array to char pointers?

Yes. Same for the array of environment variables. The kernel needs to know somehow how much data to copy from the calling process and the system call doesn't take an explicit argument for the number of elements in those arrays. (Passing that as an explicit argument would of course also work.)

1

So, first of all, there is no exec() system call. exec() is a notation used to refer to a family of related APIs: the execve() system call and a set of library functions layered on top of that system call. The library functions provide small API variations that are often useful.

Some of the library functions--the ones with an l in the suffix of the function name--are variadic functions. They allow you to specify the command-line arguments of the program that will be executed as a list (of arbitrary length) in the function call. The null pointer at the end of that list allows the function to determine where the end of that list is. (Just where these arguments are stored is implementation dependent. See Are va_arg arguments to functions stored in stack or heap memory?)

The library function internally allocates an argv array of the right size and populates that array with pointers to the string (and adds a NULL as the last element of that argv). The constructed argv is then (ultimately) passed in a call to execve().

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .