memset

Discussion in 'C Programming' started by =?ISO-8859-1?Q?Daniel_Sch=FCle?=, Apr 26, 2005.

  1. Hello

    given the signature of memset function

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

    what sense does it make to have c as an integer
    if only the least significant byte is written
    (I tried 0xFFFFFFFF und 0xFFFFFF00)

    Thx in advance

    Daniel
     
    =?ISO-8859-1?Q?Daniel_Sch=FCle?=, Apr 26, 2005
    #1
    1. Advertisements

  2. In article <d4mbr8$jlg$-karlsruhe.de>,
    =?ISO-8859-1?Q?Daniel_Sch=FCle?= <-karlsruhe.de> wrote:
    >given the signature of memset function


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


    >what sense does it make to have c as an integer
    >if only the least significant byte is written


    memset() dates from the time when characters and shorts
    were always promoted to integers for function calls. Changing the
    interface definition now would likely break code.
    --
    Warning: potentially contains traces of nuts.
     
    Walter Roberson, Apr 26, 2005
    #2
    1. Advertisements

  3. Daniel Schüle wrote on 26/04/05 :
    > Hello
    >
    > given the signature of memset function
    >
    > void *memset(void *s, int c, size_t n);
    >
    > what sense does it make to have c as an integer
    > if only the least significant byte is written
    > (I tried 0xFFFFFFFF und 0xFFFFFF00)


    Because 'char' parameter don't even exist. The minimum size for a
    parameter is int. If you declare char, actually, it is converted
    (promoted) to int. Smart guys (like the ones who have designed the
    standard C library) write 'int' instead of 'char' (in a context
    parameter). In addition, you can decide clearly if the parameter is
    signed or not (int vs unsigned) and the generated code is probably more
    compact in many cases.

    It is probably the reason why a character has the type int in C.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "There are 10 types of people in the world today;
    those that understand binary, and those that dont."
     
    Emmanuel Delahaye, May 2, 2005
    #3
  4. Walter Roberson wrote on 26/04/05 :
    > memset() dates from the time when characters and shorts
    > were always promoted to integers for function calls.


    Is it different now ?

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Clearly your code does not meet the original spec."
    "You are sentenced to 30 lashes with a wet noodle."
    -- Jerry Coffin in a.l.c.c++
     
    Emmanuel Delahaye, May 2, 2005
    #4
  5. =?ISO-8859-1?Q?Daniel_Sch=FCle?=

    Eric Sosman Guest

    Emmanuel Delahaye wrote:
    > Walter Roberson wrote on 26/04/05 :
    >
    >>memset() dates from the time when characters and shorts
    >>were always promoted to integers for function calls.

    >
    >
    > Is it different now ?


    Yes, in the presence of a prototype.

    int knr(x) short x; { ... }
    int c9x(short x) { ... }
    ...
    short s = 42;
    knr(s);
    c90(s);

    In the call to knr(), `s' is promoted to int and passed
    to the function, where it is then "demoted" back to short
    again. In the call to c9x(), `s' is passed unchanged (or
    "as if unchanged"), without promotion/demotion.

    This leads to a nasty little trap that occasionally
    snags people who are writing C9x prototypes for K&R-style
    functions, without changing the function definitions (a
    practice of dubious merit). The correct C9x declaration
    for knr() is not `int knr(short)' but `int knr(int)', and
    a too-mechanical prototype generator that produced the
    former would be wrong.

    --
     
    Eric Sosman, May 2, 2005
    #5
  6. =?ISO-8859-1?Q?Daniel_Sch=FCle?=

    Flash Gordon Guest

    Emmanuel Delahaye wrote:
    > Walter Roberson wrote on 26/04/05 :
    >
    >> memset() dates from the time when characters and shorts
    >> were always promoted to integers for function calls.

    >
    > Is it different now ?


    If the function is defined with a prototype, I believe it is. Default
    argument promotions only apply when there is no prototype.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, May 2, 2005
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. k-man
    Replies:
    4
    Views:
    4,725
    Shelley Hebert
    Dec 18, 2003
  2. Joe C
    Replies:
    5
    Views:
    9,460
    tom_usenet
    Aug 24, 2004
  3. Replies:
    17
    Views:
    7,268
    Greg Comeau
    Sep 22, 2004
  4. Guest
    Replies:
    1
    Views:
    544
    Ron Natalie
    Sep 23, 2004
  5. =?ISO-8859-1?Q?Sacha_Sch=E4r?=

    Using memset instead of a proper ctor

    =?ISO-8859-1?Q?Sacha_Sch=E4r?=, Jan 24, 2005, in forum: C++
    Replies:
    12
    Views:
    655
    Dietmar Kuehl
    Jan 24, 2005
  6. copx

    memset() and portability

    copx, Jul 24, 2003, in forum: C Programming
    Replies:
    10
    Views:
    766
    Eric Sosman
    Jul 28, 2003
  7. Mantorok Redgormor

    perils of memset

    Mantorok Redgormor, Feb 22, 2004, in forum: C Programming
    Replies:
    2
    Views:
    1,173
    Christian Bau
    Feb 22, 2004
  8. Zhiqiang Ye
    Replies:
    53
    Views:
    11,239
    Dan Pop
    Jun 28, 2004
Loading...