You can pour -1 into void * and check it out. you can return ((void *) - 1); Note that the concept of C. there is no concept of pointers in C.
If you think he's a little crazy :-), you're probably right. But there are times when this convention may be useful for error handling. And there is use of it there in important projects. In particular, the pthread library implementation uses this convention at times.
Can a pointer (address) be negative?
There is a lot to say about this.
By the concept of special interpretations of pointers by their meaning.
So there seems to be some problems about how to guarantee that (void *) - 1 is not an actual pointer. and @ictoofay seems to think that somehow I mentioned the guarantee (void *) - 1 above and how it cannot be a valid address, which I would like to clarify that I did not.
Having said that, it is assumed that a system using direct memory mapping, rather than virtual addresses. (void *) - 1 will be the most recent address in this model, and in the strict sense you should not use it. However, you can make assumptions about the storage capacity of the last physical address (see the ldl example below)
But the concept of a βspecialβ interpretation of pointers by studying value in real projects is not new. In fact, this concept is used in most modern operating systems when they manage a memory address. It is very common that the system will use addresses and address ranges and use them for "special meaning". Therefore, in fact, you, as a programmer, are really free to make any assumptions and live with their consequences. The notion that (void *) - 1 can be used as a special meaning is just around the corner. Note that this still does NOT say anything about (in?) The concept's compatibility with the fact that (void *) - 1 may be a valid address in this case.
Now to solve this "crazy" concept and why someone is using. Here are two real-world examples of where (void *) - 1 is safe.
pthreads uses it on Linux, HPUX, Solaris, and most BSDs.
In particular, PTHREAD_CANCELED is ((void *) - 1)
From pthread.h
#define PTHREAD_CANCELED ((void *) -1)
It is defined as ((void *) 1) in the darwin version of the pthread libraries. Fun fun;
In glibc (with the definition of _GNU_SOURCE), ld.so provides the ability to search for alternative values ββof the same character in dynamically loaded libraries. The value of RTLD_NEXT is (void *) - 1.
Thought of Flow C
In C there is no reason why this value should be interpreted in a certain way. C is not strongly typed and is fluid in these terms. From this comes a lot of energy.
In the "value" NULL
Could the corresponding C # define NULL implementation be something ridiculous
has a fun explanation of concepts.