Memmove, memcpy and new

I am making a simple byte buffer that stores its data in a char array acquired with new, and I am just wondering if the memcpy and memmove functions can give me something strange if they are used in memory obtained with the new or is would you recommend anything instead?

+3
source share
5 answers

No, they are beautiful. newand malloc()- these are just two different ways of using memory on the heap (in fact, they are exactly the same, because it is newused malloc()under the hood in most implementations). As soon as you have a valid variable char*in your hand (allocated new, malloc()or on the stack), it is just a pointer to memory, therefore memcpy(), other functions from this family will work as expected.

+3
source

Using memcpy()/ memmove()should work fine on these types of data. In general, you can safely use them for any type of POD.

+4
source

char .

++ std:: copy std:: copy_backward ?

+2

new memmove. , , , . PowerPC 7447, , , 16 . Altivec SIMD, memcpy , 16 . . .

? , , , . CPU, OS , (PowerPC 7447a, VxWorks 5.5, GCC 2.95), , 8 , 16 . , , . , ++ -, , .

The fact is that this can lead to a small difference in performance if you are on a certain platform and care about problems with a low level of optimization, such as alignment. You probably don't need to worry about this for most applications.

+1
source

I would still use a vector with std :: copy, but that does not mean that the new / memcpy / memmove is bad. memmove is really preferable if you insist on moving parts in the buffer.

0
source

Source: https://habr.com/ru/post/1736379/


All Articles