Thread: memset vs normal assignment

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    6

    memset vs normal assignment

    Hi!

    I'm working with Microchips USB framework on a PIC18F4550 and I'm pretty new to C. The goal is to understand how it and the PIC works.

    Compiler = XC8

    After reading some documentation about memset I'm confused with these two lines.

    Code:
    UEP0 = 0;
    
    memset((void*)&UEP1, 0x00, 0x01);
    The first line I understand clears the UEP0 register and from what I understand the second line does the same but for the UEP1 register, or am I wrong?

    If I'm not wrong, what is the difference?? Why not just use
    Code:
    UEP0 = 0;
    UEP1 = 0;
    //R

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It really depends on what UEP1 is.

    Assignment will always make it zero regardless of how large the data type is.

    memset as shown will only ever set the low byte to zero.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    6
    Quote Originally Posted by Salem View Post
    It really depends on what UEP1 is.

    Assignment will always make it zero regardless of how large the data type is.

    memset as shown will only ever set the low byte to zero.
    UEP1 is a one byte register.
    So in that case, is there a difference or is it the same?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by redsmolf View Post
    UEP1 is a one byte register.
    So in that case, is there a difference or is it the same?
    When using memset for explicitly just one byte, the difference is that using a plain old assignment statement is both shorter to write and likely faster to execute.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Normal maps: Get normal x/y/z from color
    By Devils Child in forum Game Programming
    Replies: 2
    Last Post: 08-09-2009, 12:01 PM
  2. Memset
    By DickArmy in forum C Programming
    Replies: 3
    Last Post: 06-30-2009, 04:59 PM
  3. how to use memset for int?
    By manav in forum C Programming
    Replies: 4
    Last Post: 04-12-2008, 07:15 AM
  4. memset
    By l2u in forum C Programming
    Replies: 3
    Last Post: 07-03-2006, 04:16 PM
  5. memset()
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-11-2002, 09:34 PM