Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do some C functions accept null parameters?

Tags:

c

null

pointers

I always thought that C does not accept NULL parameters, until I started learning about pointers. In some programming languages, like python for one, it is possible to pass a NULL parameter as an argument, but in C I always thought this would result in Undefined Behaviour.

My question is just one of curiosity, how can a function, such as this...

waitpid(child_pid, &status, options);  //pointer &status

...accept a NULL pointer as parameter without running into Undefined Behaviour, don't NULL pointers simply point to nothing?

Simply put, why is this acceptable in C?

like image 990
buydadip Avatar asked Dec 17 '14 18:12

buydadip


People also ask

Can you pass null to a function in C?

You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter.

How do you pass a null as a parameter?

You cannot pass the null value as a parameter to a Java scalar type method; Java scalar types are always non-nullable. However, Java object types can accept null values. The values of both variable <@I> and variable <@A> are null, since values have not been assigned to them.

How does null work in C?

The C and C++ languages have a null character (NUL), a null pointer (NULL), and a null statement (just a semicolon (;)). The C NUL is a single character that compares equal to 0. The C NULL is a special reserved pointer value that does not point to any valid data object.

How are parameters passed to functions in C?

Parameters in C functions There are two ways to pass parameters in C: Pass by Value, Pass by Reference.


2 Answers

In some programming languages [...] it is possible to pass a NULL parameter as an argument, but in C I always thought this would result in Undefined Behavior.

Passing a NULL parameter for a pointer by itself does not result in UB; it's attempting to access the memory pointed to by a pointer set to NULL that does.

Passing NULL is a very common practice for situations when something is not specified. The caller is expected to check parameters for NULL before performing the access. For example, the standard lets you pass NULL to free, which makes the function a lot more convenient.

don't NULL pointers simply point to nothing?

Yes, they do. But that "nothing" is globally well-known, so using a NULL lets you communicate the fact that a pointer points to nothing to functions that you call. In other words, the check

if (myPointer == NULL)

is well-defined*, so you can use it to your advantage.

* Unless you use a dangling pointer, i.e. a pointer that you have freed, or a pointer that points to object that went out of scope. You can prevent the first situation from happening by assigning NULL to every pointer that you free(), and the second situation by declaring pointers in the scope that has the same or higher level of nesting as the scope of an automatic object to which the pointer is pointing.

like image 80
Sergey Kalinichenko Avatar answered Sep 30 '22 18:09

Sergey Kalinichenko


void func_with_optional_arg(char *optional)
{
    if (optional == NULL) {
        // do something differently
    }

    /* ... */
}

Why would that invoke UB? Dereferencing a NULL pointer certainly would, but passing one around does not. NULL is a sentinel value used to determine whether or not a pointer is valid (not that invalid pointers cannot have other values, but we use this one explicitly.) If passing it to a function invoked UB then what would be the point of its existence in the first place?

whereas passing a NULL non-pointer value is not?

There is no such thing as a "NULL non-pointer" in C, so I'm not sure what you mean here.

like image 42
Ed S. Avatar answered Sep 30 '22 18:09

Ed S.