I am not C expert, so be polity. I do not see something similar in C world, 
but similar techniques in other languages, such like Vala.

I suggest to create two new pointer type:
1. can-be-null
2. cannot-be-null
(You must find other words to describe it behavior).

First enforces to compiler checking it is not null, when it was used (by -> 
operator) or used, when cannot-be-null is excepted. Of course, user can cast 
to cannot-be-null to avoid checking. So:
1. Usage with -> operator, or * must be placed inside conditional block, with 
contains null check in condition
2. Passing as cannot-be-null (except explicit conversion) will requires to put 
in block as above

The same restrictions apply to put normal pointer in case, when cannot-be-null 
excepted.

Additionally, break-instruction word will be reserved for functions, so exit 
will be traded as end of control-flow and further code will be traded as 
checked.

Imagine assert will be traded as break-ins

This code:

int get_vector_size(struct vector cannot-be-null *vec)
{
   assert(vec != NULL)

   return vec->size;
}

Will be correct, because assert macro will (possible) be extended to:

if (!(vec != NULL)) {
    puts("assertion error: vec != NULL");
    exit(1);
}

And exit will be break-instruction.

We could use cannot-be-null in some cases, such like:

int year;

scanf("How old are you:%d", &year);

But user could pass NULL as second parameter.

When pointers in scanf parameters are cannot-be-null, the compiler will 
disallow pass null by us.

Of course - inside scanf must check if each parameter are not NULL.

Another code:

void error(const char *message)
{
   perror(message);
}

And perror will have cannot-be-null word describe one's parameter.
This will cause compile-time-error, because message was passed to function, 
which parameter is cannot-be-null.

Programmer should do:
void error(const char *message)
{
   if (NULL != message)
     perror(message);
}

What do you think. This will solve a lot of problems,

It could be also great to introduce function checking if any of it parameter 
are null, but I do not known how to make it good-quality (simple in usage). 
Programer should propably pass number of passed parameters, or type of 
parameter before each.


Reply via email to