Setting extra bits in a bool makes it true and false at the same time

In C++ the bit representation (and even the size) of a bool is implementation defined; generally it’s implemented as a char-sized type taking 1 or 0 as possible values. If you set its value to anything different from the allowed ones (in this specific case by aliasing a bool through a char and modifying its … Read more

why is data structure alignment important for performance?

Alignment helps the CPU fetch data from memory in an efficient manner: less cache miss/flush, less bus transactions etc. Some memory types (e.g. RDRAM, DRAM etc.) need to be accessed in a structured manner (aligned “words” and in “burst transactions” i.e. many words at one time) in order to yield efficient results. This is due … Read more

Why can a T* be passed in register, but a unique_ptr cannot?

Is this actually an ABI requirement, or maybe it’s just some pessimization in certain scenarios? One example is System V Application Binary Interface AMD64 Architecture Processor Supplement. This ABI is for 64-bit x86-compatible CPUs (Linux x86_64 architecure). It is followed on Solaris, Linux, FreeBSD, macOS, Windows Subsystem for Linux: If a C++ object has either … Read more

C++ on x86-64: when are structs/classes passed and returned in registers?

The ABI specification is defined here. A newer version is available here. I assume the reader is accustomed to the terminology of the document and that they can classify the primitive types. If the object size is larger than two eight-bytes, it is passed in memory: struct foo { unsigned long long a; unsigned long … Read more

How do C compilers implement functions that return large structures?

None; no copies are done. The address of the caller’s Data return value is actually passed as a hidden argument to the function, and the createData function simply writes into the caller’s stack frame. This is known as the named return value optimisation. Also see the c++ faq on this topic. commercial-grade C++ compilers implement … Read more

Are there any downsides to passing structs by value in C, rather than passing a pointer?

For small structs (eg point, rect) passing by value is perfectly acceptable. But, apart from speed, there is one other reason why you should be careful passing/returning large structs by value: Stack space. A lot of C programming is for embedded systems, where memory is at a premium, and stack sizes may be measured in … Read more

Why does the Mac ABI require 16-byte stack alignment for x86-32?

From “IntelĀ®64 and IA-32 Architectures Optimization Reference Manual”, section 4.4.2: “For best performance, the Streaming SIMD Extensions and Streaming SIMD Extensions 2 require their memory operands to be aligned to 16-byte boundaries. Unaligned data can cause significant performance penalties compared to aligned data.” From Appendix D: “It is important to ensure that the stack frame … Read more