-1

I have function which copies of array of integer into vector which is working fine with std::copy call but when I changed std::copy to std::memcpy its not working correctly. Can someone plz point out what I am doing wrong ?

 void Walle_SSD1306::RadarD(uint8_t *RadarLL, uint8_t isFirstRadarLogo)
 {
 //Following two copy statement is working correctly
 std::copy(
    RadarLL+128
    , RadarLL + 8192
    , buffer.begin()+128
    );
 std::copy(
    RadarLL+38
    , RadarLL + 90
    , buffer.begin()+38
    );

//But when I change copy to memcpy following code is not working correctly  
std::memcpy(
     (buffer.begin()+128) , (RadarLL+128),  (8192 - 128)*sizeof(uint8_t)
    );
std::memcpy(
    (buffer.begin()+38), (RadarLL+38), (52*sizeof(uint8_t))
    );

 //where buffer is...
 std::vector<uint8_t> buffer;
  • Such an expression buffer.begin()+128 used in the memcpy is invalid. You should use at least &bffer[128] or buffer.data() + 128 – Vlad from Moscow Mar 03 '17 at 00:12
  • When you say "not working" we have no idea what that means. Does it even compile? – John Zwinck Mar 03 '17 at 00:14
  • Code is getting compiled but buffer is not getting correct value from RadarLL. Can you point out any mistake I am doing ? – user5572904 Mar 03 '17 at 01:28
  • 3
    Guess you assume that `memcpy` will magically be a lot faster than `std::copy`. You better prepare yourself for some disappointment. – Bo Persson Mar 03 '17 at 02:21

2 Answers2

1

std::copy can work with iterators and supports buffers overlap memcpy only works with pointers

vadim
  • 178
  • 1
  • 9
0

Using the expression buffer.begin()+128 in the memcpy is invalid because in general this expression yields an iterator.

You should use pointers. For example

std::memcpy(
     (buffer.data()+128) , (RadarLL+128),  (8192 - 128)*sizeof(uint8_t)
    );

or

std::memcpy(
     &buffer[128] , (RadarLL+128),  (8192 - 128)*sizeof(uint8_t)
    );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335