memcpy vs assignment in C

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 … Read more

Which is best for data store Struct/Classes?

I would make the choice based on the following criteria reference type vs value type semantics. If 2 objects are only equal if they are the same object, it indicates reference type semantics => class. If the value of its members defines equality (e.g. 2 DateTimes are equal if both represent the same point in … Read more

What is the difference between using a struct with two fields and a pair?

std::pair provides pre-written constructors and comparison operators. This also allows them to be stored in containers like std::map without you needing to write, for example, the copy constructor or strict weak ordering via operator < (such as required by std::map). If you don’t write them you can’t make a mistake (remember how strict weak ordering … Read more

When extending a padded struct, why can’t extra fields be placed in the tail padding?

Short answer (for the C++ part of the question): The Itanium ABI for C++ prohibits, for historical reasons, using the tail padding of a base subobject of POD type. Note that C++11 does not have such a prohibition. The relevant rule 3.9/2 that allows trivially-copyable types to be copied via their underlying representation explicitly excludes … Read more

How is the result struct of localtime allocated in C?

The pointer returned by localtime (and some other functions) are actually pointers to statically allocated memory. So you do not need to free it, and you should not free it. This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content … Read more

Initializing an Array of Structs in C#

Firstly, do you really have to have a mutable struct? They’re almost always a bad idea. Likewise public fields. There are some very occasional contexts in which they’re reasonable (usually both parts together, as with ValueTuple) but they’re pretty rare in my experience. Other than that, I’d just create a constructor taking the two bits … Read more