How do realloc and memcpy work?

前端 未结 8 1057
遇见更好的自我
遇见更好的自我 2020-12-01 02:49

I have two questions.

  1. Do realloc() and memcpy() copy the entries in an array to another in a way faster than just iterating on eac

8条回答
  •  -上瘾入骨i
    2020-12-01 03:28

    The x86 has special instructions for scanning and matching a byte/word in a block of memory as well and one that can be used to copy a block of memory (it is a CISC cpu after all). A lot of C compilers that implement inline assembly language and a pragma to do inlining of entire functions have for many many years taken advantage of this in their library functions.

    The ones used for mem copy are movsb/movsw in combination to the rep instruction.

    CMPS/MOVS/SCAS/STOS
    REP, REPE, REPNE, REPNZ, REPZ
    

    Setup registers with src/trg addresses and int count and away you go.

提交回复
热议问题