Question about memset

This is a discussion on Question about memset within the C Programming forums, part of the General Programming Boards category; According to link , memset accepts an int for the value to be set. Why an int, if memset set ...

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    Question about memset

    According to link, memset accepts an int for the value to be set. Why an int, if memset set each byte in the buffer?

    I can only imagine that memcpy tries to optimize by copying 4 bytes at a time.

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The reason for taking an int is probably hysterical^Whistorical - someone wrote it that way a long time ago, and it's still that way.

    It really doesn't make much difference, the value is either passed on the stack as a 4-byte aligned value (in a 32-bit processor), or it is passed in a register. Either way, a unsigned char or some such would take up the same resource (memory or register).

    Any decision to optimize it by writing multiple bytes at a time is purely internal to memset, and completely irrelevant to what parameters it takes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I am asking this because I never paid attention to this paramter. But recently I saw that a developer in my team changed memset call in a legacy code that I am working from:
    Code:
    memset(buff, 0xFF, size)
    To:

    Code:
    memset(buff, 0xFFFFFFFF, size)
    Which seems pointless...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That will have no effect on the actual content in buff at all - both will do the same thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange memset question
    By Death_Wraith in forum Game Programming
    Replies: 14
    Last Post: 09-25-2004, 12:58 AM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Simple Question on Correct Usage of memset
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 03-16-2004, 07:58 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21