View Single Post
Old 08-18-2002, 12:21 PM   #3 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
The quick and dirty... Void tells the compiler that it shouldn't make any assumptions. There is a real long explination of it's purpose in the ANSI document, but it's hard to follow, so bare with me while I try and put it into realistic terms by use of example:
Code:
int intFun() { return 4; }
The compiler knows that this function returns an int, most likely 32 bits of data. This can be turned into the following assembly:
Code:
intFun:
mov eax, 0x00000004
push eax
ret
Now with void:
Code:
void voidFun() { // blah }
Evaluates to:
Code:
voidFun:
nop
ret
Now void pointers are another story altogether, and this is where compiler writers have to be careful, at least with ANSI C. C++ is another story and void *'s really aren't even useful any longer. So, let's see this in C:
Code:
char *cp;
int *ip;
void *vp;
int ival;

ival = 4;
ip = &ival;
cp = ip; /* this should be a compiler error, but is generally just a warning. */
cp = (char*)ip; /* okay, but bad style.  No warning, the compiler presumes that since you casted it you know what you're doing. */
vp = ip; /* perfectly okay. */
vp = cp; /* perfectly okay. */
cp = vp; /* also okay. */
ip = vp; /* again, okay. */
So basically, the compiler is instructed to morph void pointers to whatever they are representing. In almost every case the void pointers, char pointers, and int pointers are exactly the same size (based on the architecture, 32 bits for Intel and generally 64 bit for modern Sparc). The authors of the standard however did take into consideration that some older platforms (I think VMS??) did not operate in the same way and actually had pointers of different size. The compiler HAD to be aware of this and HAD to function with it.

All that said, compiler writers have generally disregarded these rules.

In C++ you have meta programming so void* is worthless. Templates make life grand!
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote