memset() function - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

memset() function

sureshNsureshN Posts: 1Member
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

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    : 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
  • LundinLundin Posts: 3,711Member
    : In C and C++ char parameters are always promoted to integers to
    : speed up accessing them. Yes, they could have declared it as char
    : but it would cause a few more assembly instructions, and when memset
    : was first introduced into the language over 20 years ago computers
    : were much much slower then they are today, so every nanosecond was
    : important.


    Still, this is a very dumb function prototype. If the CPU is using a certain bus size, it is up to the compiler to optimize the parameters and put them in blocks of 16 or 32 bits. Because... what happens if I decide to write code for the most common computer used in the world, an 8-bit microcontroller? The program gets slower because I must pass an additional junk byte to the stack.

    So in my opinion, the answer to the question why they used an int for memset is: because they weren't thinking in a rational and system-independant way when they wrote the C standard. This particular function is no doubt written to suit ancient 16-bit Solaris computers.
  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : So in my opinion, the answer to the question why they used an int
    : for memset is: because they weren't thinking in a rational and
    : system-independant way when they wrote the C standard. This
    : particular function is no doubt written to suit ancient 16-bit
    : Solaris computers.

    Its up to the compiler to determine the size of any object other than char. So a compiler targeting an 8-bit processor could easily make sizeof(int) = 1 instead of 2, 4 or something larger. If that is the case (I don't know whether it is nor not) then you don't have an argument.

    The C language was originally written by K&R to write the Unix operating system, but I don't know what kind of computer they used. If the memset function was going to present a problem on other platforms then it would have shown up sometime in the past 30 years or so and would probably have been changed by now. I'm not about to out-guess all those Ph.D's so I'd say memset() is pretty darned stable and working correctly as intended.
    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • LundinLundin Posts: 3,711Member
    : Its up to the compiler to determine the size of any object other
    : than char. So a compiler targeting an 8-bit processor could easily
    : make sizeof(int) = 1 instead of 2, 4 or something larger. If that
    : is the case (I don't know whether it is nor not) then you don't have
    : an argument.

    Nope they can't make it 8-bit... ANSI C 5.2.4.2.1 "Sizes of integer types" states:

    "Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign."

    And then they say that the minimum and maxiumum values of and int should be -32767 and 32767. So ANSI C does not allow 8-bit int. The above was however taken from C99, but I'm pretty sure C89 says the very same.
Sign In or Register to comment.