Archived

This topic is now archived and is closed to further replies.

spetnaz_

NULL and void

Recommended Posts

wat is the diference between NULL and void. i know that void is a data type but there could only be one variable for this type: NULL, so what is the point of having void? as far as i c the reason is that because functions have to have a data type not a variable as there return or input value, that is u cant use NULL in void''s place, but still what is the point of having void when NULL could have done exactly the same thing? what do u think

Share this post


Link to post
Share on other sites
void is a pseudo datatype. You can''t have variables of type null (except for very specific template circumstances), although you can have null pointers. (not sure if you can have null references; anyone with too much time on their hands wanna try?)

NULL is not a void value, anyway. It''s used as a pointer. Basically, it''s a pointer value which is not equal to any valid pointer.

void is used in these circumstances:
  • Saying that a function doesn''t return anything.
  • Saying that a function takes no arguments (this is optional, and a throwback to C)
  • void* is a pointer which doesn''t assume anything about the memory it''s pointing to.



How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
ok, first of all, you can NOT have NULL references, the program would be very dangerouse and would probably crash if you tried to use that reference. If you have a function for example that returns a reference and you return a local variable, your compiler will flag this as the local variable will no longer exist and therefor you would have a invalid reference.

void is actually a data type, such as int or float. NULL is a value. Here is it''s declaration in windef.h

#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif

Hope that answers your question.

Share this post


Link to post
Share on other sites
void is a great data type! Like... what about those memcpy routines you use:

void memcpy(void *dst, void *src, unsigned long Size);

Without voids, you''d have to write it for each data type, OR use a template (which does the same thing as you doing it manually).

NULL is simply a value that you can set other things to, while void is a data type of an unknown or non-existant variable . If you wanted to point to a memory location without care of what type it is storing: Say you have a memory pool, you don''t need a specific type for it, or the entries if you want to track them, so you can use void pointers to store info about it. You could also use void pointers for things like garbage collection, and memory tracking... say you want to track all the memory you''re allocating, no matter what type it is, and put it into a single list? Simple, use a list of void pointers to keep track of what memory is allocated/deallocated.

Share this post


Link to post
Share on other sites
quote:
Original post by baldurk
quote:
Original post by desertcube
ok, first of all, you can NOT have NULL references, the program would be very dangerouse and would probably crash if you tried to use that reference.

Surely the same applies to null pointers?


Almost, except you can check a pointer more easily than a reference .



void SomeFunction( void *TestData)
{
if (TestData==NULL) //Check for NULL pointer

return; //nothing here, lets return early

}


You "can" check null references also... but not many people ever do something like this:

void SomeFunction(long &TestData)
{
if (&TestData==NULL) //Check if the address of testdata is null

return; //Yup, lets return

}

Share this post


Link to post
Share on other sites
quote:
Original post by Jason2Jason
I forgot, can you have void references? Or is it just recommended to stick with void pointers if using void as a datatype?

-J


I don''t beleive a void reference is possible... simply because there is no such data type. You cannot use a void reference directly, and that was the point of references, so I don''t think something like this would compile: void Test(void &t);

Share this post


Link to post
Share on other sites