memcpy vs assignment in C

You should never expect them to 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 … Read more

memcpy zero bytes into const variable – undefined behavior?

c c17 The older question Is it guaranteed to be safe to perform memcpy(0,0,0)? points out 7.1.4p1: Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as a value outside the domain of the function, or a … Read more

C++ memcpy return value

If a function has nothing specific to return, it is often customary to return one of the input parameters (the one that is seen as the primary one). Doing this allows you to use “chained” function calls in expressions. For example, you can do char buffer[1024]; strcat(strcpy(buffer, “Hello”), ” World”); specifically because strcpy returns the … Read more

Is the std::array bit compatible with the old C array?

This doesn’t directly answer your question, but you should simply use std::copy: T c[N]; std::array<T, N> cpp; // from C to C++ std::copy(std::begin(c), std::end(c), std::begin(cpp)); // from C++ to C std::copy(std::begin(cpp), std::end(cpp), std::begin(c)); If T is a trivially copyable type, this’ll compile down to memcpy. If it’s not, then this’ll do element-wise copy assignment and … Read more

Does “&s[0]” point to contiguous characters in a std::string?

A std::string’s allocation is not guaranteed to be contiguous under the C++98/03 standard, but C++11 forces it to be. In practice, neither I nor Herb Sutter know of an implementation that does not use contiguous storage. Notice that the &s[0] thing is always guaranteed to work by the C++11 standard, even in the 0-length string … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)