01 January 2009

bzero() and memset()

bzero() and memset() are two functions used for similar use - to set or initialize the values of a memory segment to zero (or NULL). Where bzero() is used only for setting the values to zero, memset() is also used for initializing the values to some others values too.
the function prototype for both are like as below:

#include 
void bzero(void *mem, int bytes);
void* memset(void *mem, int val, int bytes);
 
mem : the memory segment to initialize, holds the address of the starting block.
bytes : Size of the memory segment.
val :  value to be filled in the segment.
 
(both these functions are defined in the string.h header file, and hence it needs to be included
whenever these are included in the file.)
 
 
These two functions have been historically been used in an interchangable manner, the reason being the
nature of UNIX and its vastness. bzero() was originally a part of the BSD-Unix, and memset() was a part of
the AT&T Unix. And later memset() was also adopted in the ANSI C and POSIX standards whereas bzero()
was depracated. And while writing professional codes it is not advisable to use bzero() and longer, instead
memset() should be used. (Although both will remain to be supported in the all the platforms).
 

4 comments:

Anonymous said...

Thanks for the explanation, especially the deprecation note on bzero()

Unknown said...

Cảm ơn bạn rất nhiều!

Anonymous said...

Thank you

Unknown said...

Thanks a lot !!