c - Please look into this inexplicable behavior and output of memcpy() for overlapping memory blocks -


after reading following memcpy(), proceeded read memmove():

to avoid overflows, size of arrays pointed both destination , source parameters, shall @ least num bytes, , should not overlap (for overlapping memory blocks, memmove safer approach).(link)

and after checking program used illustrate working of memmove() decided tweak using memcpy() instead see how different output.to surprise,they same if it's case of overlapping memory blocks.here program , output,and have proceeded describe confusion after that:

#include <stdio.h> #include <string.h>  int main () {   char str[] = "memmove can useful......";   //memmove (str+20,str+15,11);   memcpy(str+20,str+15,11);  //simply used memcpy instead of memmove   puts (str);   return 0; } 

output memmove can very useful.

this output same memmove().and here confusions:

1) why output same both?since there no intermediate buffer used in case of memcpy(),i expect copy start copying character in str+15 position str+20 position (overwriting there),character in str+16 position str+21 position, , on till character in str+20 position,which has changed character in str+15 position, copied str+25 position.but it's not so,there no overwriting , it's acting if intermediate buffer used write exact original string.here's illustration:

memmove can useful......  //original positions before memcopy                ^    ^             str+15  str+20  memmove can vseful......                     ^ copies str+15 str+20  memmove can veeful......                      ^ copies str+16 str+21 memmove can verful......                       ^ copies str+17 str+22 memmove can veryul......                        ^copies str+18 str+23 memmove can very l......                         ^ copies str+19 str+24 memmove can very v......                          ^ expect 'v' copied str+20 str+25                            str+20  has 'v',not 'u' memmove can very ve.....                           ^ expect 'e' copied str+21 str+26                              str+21 has 'e' not 's' 

then why memcpy() copying memmove can very useful, instead of memmove can very v ?

2) minor secondary question arising it.the following said memmove() (link)

copying takes place if intermediate buffer used, allowing destination , source overlap.

what as if here?isn't intermediate buffer really used memmove()?

if objects overlap, behaviour of memcpy undefined. there's little point in trying reason undefined behaviour. since undefined defies reason. know rules, , documented. if objects overlap, use memmove.

as use of "as if", specify behaviour not place limitations on implementation. allows library implementor use whatever method see fit, long end result same using intermediate buffer. example, implementation detect objects not overlap , avoid using intermediate buffer performance reasons.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -