memcpy vs assignment in C

Under what circumstances should I expect memcpys to outperform assignments on modern INTEL/AMD hardware? I am using GCC 4.2.x on a 32 bit Intel platform (but am interested in 64 bit as well).

memcpy vs assignment in C — should be memmove?

As pointed out in an answer to this question, the compiler (in this case gcc-4.1.2, yes it’s old, no I can’t change it) can replace struct assignments with memcpy where it thinks it is appropriate. I’

C structure assignment uses memcpy

I have this StructType st = StructTypeSecondInstance->st; and it generates a segfault. The strange part is when the stack backtrace shows me: 0x1067d2cc: memcpy + 0x10 (0, 10000, 1, 1097a69c, 11db0

Copying array of structs with memcpy vs direct approach [duplicate]

This question already has an answer here: memcpy vs assignment in C 1 answer Suppose pp is a pointer to an array of structs of length n. [was dynamically allocated] Suppose I want to create a c

std::shared_ptr assignment of data vs. memcpy

I am using std::shared_ptr in C++11 and I would like to understand if it’s better to assign structures of type T in this way: T a_data; std::shared_ptr<T> my_pointer(new T); *my_pointer = a_data

C: Memcpy vs Shifting: Whats more efficient?

I have a byte array containing 16 & 32bit data samples, and to cast them to Int16 and Int32 I currently just do a memcpy with 2 (or 4) bytes. Because memcpy is probably isn’t optimized for lenghts

Copying structure in C with assignment instead of memcpy()

Up until recently, I have only seen copying of structure fields done with memcpy(). In classes and online instructions, copying the contents of one struct into another generally looks like struct bloc

C++ memcpy() vs std::copy()

Is it better to use memcpy as shown below or is it better to use std::copy() in terms to performance? Why? char *bits = NULL; … bits = new (std::nothrow) char[((int *) copyMe->bits)[0]]; if (bits

Memcpy or struct assignment?

I have the following code and am unsure of whether to use structure alignment or memcpy to copy struct A onto the custom stack char/byte array. Is there anything advantageous/disadvantageous about t

Delphi CopyMemory vs C++ memcpy

OK – So I never seem to have a simple question. I have a custom library (which is not written by me) written in C++. This library does some network comms and I just pass some data to it as a byte arr

strcpy vs. memcpy

What is the difference between memcpy() and strcpy()? I tried to find it with the help of a program but both are giving the same output. int main() { char s[5]={‘s’,’a’,’/0′,’c’,’h’}; char p[5]; char


You should never expect them outperform assignments. The reason is, the compiler will use memcpy anyway when it thinks it would be faster (if you use optimize flags). If not and if the structure is reasonable small that it fits into registers, direct register manipulation could be used which wouldn’t require any memory access at all.

GCC has special block-move patterns internally that figure out when to directly change registers / memory cells, or when to use the memcpy function. Note when assigning the struct, the compiler knows at compile time how big the move is going to be, so it can unroll small copies (do a move n-times in row instead of looping) for instance. Note -mno-memcpy:

    Force (do not force) the use of "memcpy()" for non-trivial block moves.  
    The default is -mno-memcpy, which allows GCC to inline most constant-sized copies.

Who knows it better when to use memcpy than the compiler itself?