Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you're right, it's not possible to implement <code>memmove</code> efficiently in standard C.</p> <p>The only truly portable way to test whether the regions overlap, I think, is something like this:</p> <p>[Edit: looking at this much later, I think it should be <code>dst+len-1</code> at the end of the second line. But I can't be bothered to test it so I'll leave as it is for now, it's possible I knew what I was talking about the first time.]</p> <pre><code>for (size_t l = 0; l &lt; len; ++l) { if (src + l == dst) || (src + l == dst + len) { // they overlap, so now we can use comparison, // and copy forwards or backwards as appropriate. ... return dst; } } // No overlap, doesn't matter which direction we copy return memcpy(dst, src, len); </code></pre> <p>You can't implement either <code>memcpy</code> or <code>memmove</code> all <em>that</em> efficiently in portable code, because the platform-specific implementation is likely to kick your butt whatever you do. But a portable <code>memcpy</code> at least looks plausible.</p> <p>C++ introduced a pointer specialization of <code>std::less</code>, which is defined to work for any two pointers of the same type. It might in theory be slower than <code>&lt;</code>, but obviously on a non-segmented architecture it isn't. </p> <p>C has no such thing, so in a sense, the C++ standard agrees with you that C doesn't have enough defined behaviour. But then, C++ needs it for <code>std::map</code> and so on. It's much more likely that you'd want to implement <code>std::map</code> (or something like it) without knowledge of the implementation than that you'd want to implement <code>memmove</code> (or something like it) without knowledge of the implementation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload