Thread: memset() function

  1. #1
    Registered User
    Join Date
    May 2007

    Question memset() function

    The prototype for the function goes like this and it fills the first n bytes of the memory area pointed to be s with the constant byte c.

    void *memset(void *s, int c, size_t n);

    If you look at the implementation of the function, the second parameter is converted to ‘unsigned char’ so as to fill the memory with byte-wise.

    I have few questions regarding the design of this function:

    -Why does the function take integer as the parameter and later converts it into unsigned char? The function could have accepted the parameter as ‘char’ directly.

    -Does it not add confusion to the user of this function to look at the parameter as integer and then pass the integer and expect the same to be filled in the memory? For example considering integer of 4 bytes, the user might pass 0xAABBCCDD to be filled in the memory bytes but the function truncates and fill 0xDD in the memory.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    It's probably a relic of the past, when C didn't have function prototypes, and every parameter was promoted to int.

    As for the other problem, you shouldn't be calling the function without first reading the manual page for it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
Website Security Test