Looking to get the fundamentals on where the term "void" comes from, and why it is called void. The intention of the question is to assist someone who has no C experience, and is suddenly looking at a C-based codebase.
相关问题
- Sorting 3 numbers without branching [closed]
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
Think of void as the "empty structure". Let me explain.
Every function takes a sequence of parameters, where each parameter has a type. In fact, we could package up the parameters into a structure, with the structure slots corresponding to the parameters. This makes every function have exactly one argument. Similarly, functions produce a result, which has a type. It could be a boolean, or it could be float, or it could be a structure, containing an arbitrary set of other typed values. If we want a languge that has multiple return values, it is easy to just insist they be packaged into a structure. In fact, we could always insist that a function returned a structure. Now every function takes exactly one argument, and produces exactly one value.
Now, what happens when I need a function that produces "no" value? Well, consider what I get when I form a struct with 3 slots: it holds 3 values. When I have 2 slots, it holds two values. When it has one slot, one value. And when it has zero slots, it holds... uh, zero values, or "no" value". So, I can think of a function returning void as returning a struct containing no values. You can even decide that "void" is just a synonym for the type represented by the empty structure, rather than a keyword in the language (maybe its just a predefined type :)
Similarly, I can think of a function requiring no values as accepting an empty structure, e.g., "void".
I can even implement my programming language this way. Passing a void value takes up zero bytes, so passing void values is just a special case of passing other values of arbitrary size. This makes it easy for the compiler to treat the "void" result or argument. You probably want a langauge feature that can throw a function result away; in C, if you call the non-void result function foo in the following statement: foo(...); the compiler knows that foo produces a result and simply ignores it. If void is a value, this works perfectly and now "procedures" (which are just an adjective for a function with void result) are just trivial special cases of general functions.
Void* is a bit funnier. I don't think the C designers thought of void in the above way; they just created a keyword. That keyword was available when somebody needed a point to an arbitrary type, thus void* as the idiom in C. It actually works pretty well if you interpret void as an empty structure. A void* pointer is the address of a place where that empty structure has been put.
Casts from void* to T* for other types T, also work out with this perspective. Pointer casts are a complete cheat that work on most common architectures to take advantage of the fact that if a compound type T has an element with subtype S placed physically at the beginning of T in its storage layout, then casting S* to T* and vice versa using the same physical machine address tends to work out, since most machine pointers have a single representation. Replacing the type S by the type void gives exactly the same effect, and thus casting to/from void* works out.
The PARLANSE programming language implements the above ideas pretty closely. We goofed in its design, and didn't pay close attention to "void" as a return type and thus have langauge keywords for procedure. Its mostly just a simple syntax change but its one of things you don't get around to once you get a large body working code in a language.
I have always taken it to mean absent. Here are four cases in the C language that matches to this use of absent
R f(void)
- Function parameters are absentvoid f(P)
- Return value is absentvoid *p
- Type of what is pointed to is absent(void) p
- Usage of value is absentOther C descendants use it for other things. The
D
programming language uses it for cases where an initializer is absentT t = void;
- initializing value is absentIt means "no value". You use void to indicate that a function doesn't return a value or that it has no parameters or both.It's much consistent with typical uses of word void in English.
Void should not be confused with null. Null means for the variable whose address is on stack, the value on the heap for that address is empty.
It means "no value". You use
void
to indicate that a function doesn't return a value or that it has no parameters or both. Pretty much consistent with typical uses of word void in English.In c# you'd use the void keyword to indicate that a method does not return a value:
Basically it means "nothing" or "no type"
There are 3 basic ways that void is used:
Function argument:
int myFunc(void)
-- the function takes nothing.Function return value:
void myFunc(int)
-- the function returns nothingGeneric data pointer:
void* data
-- 'data' is a pointer to data of unknown type, and cannot be dereferencedNote: the
void
in a function argument is optional in C++, soint myFunc()
is exactly the same asint myFunc(void)
, and it is left out completely in C#. It is always required for a return value.