memset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Schüle

    memset

    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

  • Walter Roberson

    #2
    Re: memset

    In article <d4mbr8$jlg$1@n ews2.rz.uni-karlsruhe.de>,
    =?ISO-8859-1?Q?Daniel_Sch= FCle?= <uval@rz.uni-karlsruhe.de> wrote:[color=blue]
    >given the signature of memset function[/color]
    [color=blue]
    >void *memset(void *s, int c, size_t n);[/color]
    [color=blue]
    >what sense does it make to have c as an integer
    >if only the least significant byte is written[/color]

    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.

    Comment

    • Emmanuel Delahaye

      #3
      Re: memset

      Daniel Schüle wrote on 26/04/05 :[color=blue]
      > 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)[/color]

      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."

      Comment

      • Emmanuel Delahaye

        #4
        Re: memset

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

        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++

        Comment

        • Eric Sosman

          #5
          Re: memset



          Emmanuel Delahaye wrote:[color=blue]
          > Walter Roberson wrote on 26/04/05 :
          >[color=green]
          >>memset() dates from the time when characters and shorts
          >>were always promoted to integers for function calls.[/color]
          >
          >
          > Is it different now ?[/color]

          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@sun .com

          Comment

          • Flash Gordon

            #6
            Re: memset

            Emmanuel Delahaye wrote:[color=blue]
            > Walter Roberson wrote on 26/04/05 :
            >[color=green]
            >> memset() dates from the time when characters and shorts
            >> were always promoted to integers for function calls.[/color]
            >
            > Is it different now ?[/color]

            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.

            Comment

            Working...