what is the use of memset ?

Hi all,

Please any one give best use of memset() function..

thanks
- sri

Comments

  • : Hi all,
    :
    : Please any one give best use of memset() function..
    :
    : thanks
    : - sri
    :
    :

    initialize a buffer to all known values -- normally 0.
    [code]
    char buf[255];
    memset(buf,0,sizeof(buf));
    [/code]
  • : : Hi all,
    : :
    : : Please any one give best use of memset() function..
    : :
    : : thanks
    : : - sri
    : :
    : :
    :
    : initialize a buffer to all known values -- normally 0.
    : [code]
    : char buf[255];
    : memset(buf,0,sizeof(buf));
    : [/code]
    :

    Thanks for the reply.

    Is there any other way available for initializing the buffer ?



  • : Is there any other way available for initializing the buffer ?

    certainly. here are some
    [code]
    char buffer[BUFSIZ];
    // initialize it
    for(int i = 0; i < BUFSIZE; i++)
    buffer[i] = 0;

    or

    char buffer[] = "Hello World";

    or
    char buffer[255] = {0} // only 1st byte set to 0
    [/code]
  • I managed to use the memset()... but what the arguments signifies?
    memset(buf, 0, sizeof(buf)) : why the 0 ? can we any other integer ? what happens when we use any other integer ?

    - sri



    : : Is there any other way available for initializing the buffer ?
    :
    : certainly. here are some
    : [code]
    : char buffer[BUFSIZ];
    : // initialize it
    : for(int i = 0; i < BUFSIZE; i++)
    : buffer[i] = 0;
    :
    : or
    :
    : char buffer[] = "Hello World";
    :
    : or
    : char buffer[255] = {0} // only 1st byte set to 0
    : [/code]
    :

  • : I managed to use the memset()... but what the arguments signifies?
    : memset(buf, 0, sizeof(buf)) : why the 0 ? can we any other integer ? what happens when we use any other integer ?
    :
    : - sri

    you [b]could[/b] use other integers, but 0 is the normal one, and is used by all standard c and c++ libraries to indicate "no data" or "end of string". You can use any value between 0 and 255, but their significance would only be in your program. You can't use values outside that range because a char data type will not hold them.
  • Whether the 0 is used for filling all the elements in the buffer ? or it specifies the starting element from which we have to initialize the memory ? This memset applicable only for the character buffers or can we use it for any pointer ?

    thanks for the reply.

    - sri



    : : I managed to use the memset()... but what the arguments signifies?
    : : memset(buf, 0, sizeof(buf)) : why the 0 ? can we any other integer ? what happens when we use any other integer ?
    : :
    : : - sri
    :
    : you [b]could[/b] use other integers, but 0 is the normal one, and is used by all standard c and c++ libraries to indicate "no data" or "end of string". You can use any value between 0 and 255, but their significance would only be in your program. You can't use values outside that range because a char data type will not hold them.
    :

  • : Whether the 0 is used for filling all the elements in the buffer ? or it specifies the starting element from which we have to initialize the memory ? This memset applicable only for the character buffers or can we use it for any pointer ?
    :

    memset() can be used to initialize arrays of any type -- just don't use it on c++ classes! Using values other than 0 will not work if you attempt to initialize an int array with memset(). If you want it initialized with some value other than 0 you will have to do it the hard way with a loop.
    [code]
    int array[255];
    // initialize all elements to 0
    memset((char *)array,0,sizeof(array));
    [/code]

  • memset(*destination, value_stored_in_destination, Size_of_value);

    for eg:

    char buf[100];
    memset(buf, 5 , 5);

    Ans : buf[0], buf[1], buf[2], buf[3], buf[4] all are initiallized to Zero.

    I hope that memset is a function that will did...

    KNDLY TELL ANY OTHER SUGGESTIONS .. PLS
    Muthukumar Pavel
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories