July 1st, 2024

I _____ hate arrays in C++

The article explores challenges in using arrays in C++, focusing on array-to-pointer conversion pitfalls, differences from pointers, and practical examples of errors. Caution and awareness are advised for C++ developers.

Read original articleLink Icon
I _____ hate arrays in C++

The article discusses the challenges and nuances of using arrays in C++. It highlights the array-to-pointer conversion, where an array can be mistakenly treated as a pointer, leading to lost information and unexpected behavior. The author emphasizes the differences between arrays and pointers, cautioning against assuming their interchangeability. Various scenarios are presented where array-to-pointer conversion occurs or doesn't occur, affecting functions, operators, and templates differently. The text delves into practical examples illustrating potential pitfalls when working with arrays, such as incorrect element traversal and unexpected padding bytes due to compiler optimizations. The importance of understanding these concepts for C++ developers is underscored, urging caution and awareness when dealing with arrays in the language.

Related

Own Constant Folder in C/C++

Own Constant Folder in C/C++

Neil Henning discusses precision issues in clang when using the sqrtps intrinsic with -ffast-math, suggesting inline assembly for instruction selection. He introduces a workaround using __builtin_constant_p for constant folding optimization, enhancing code efficiency.

Optimizing the Roc parser/compiler with data-oriented design

Optimizing the Roc parser/compiler with data-oriented design

The blog post explores optimizing a parser/compiler with data-oriented design (DoD), comparing Array of Structs and Struct of Arrays for improved performance through memory efficiency and cache utilization. Restructuring data in the Roc compiler showcases enhanced efficiency and performance gains.

Optimizing JavaScript for Fun and for Profit

Optimizing JavaScript for Fun and for Profit

Optimizing JavaScript code for performance involves benchmarking, avoiding unnecessary work, string comparisons, and diverse object shapes. JavaScript engines optimize based on object shapes, impacting array/object methods and indirection. Creating objects with the same shape improves optimization, cautioning against slower functional programming methods. Costs of indirection like proxy objects and function calls affect performance. Code examples and benchmarks demonstrate optimization variances.

Weekend projects: getting silly with C

Weekend projects: getting silly with C

The C programming language's simplicity and expressiveness, despite quirks, influence other languages. Unconventional code structures showcase creativity and flexibility, promoting unique coding practices. Subscription for related content is encouraged.

The Byte Order Fiasco

The Byte Order Fiasco

Handling endianness in C/C++ programming poses challenges, emphasizing correct integer deserialization to prevent undefined behavior. Adherence to the C standard is crucial to avoid unexpected compiler optimizations. Code examples demonstrate proper deserialization techniques using masking and shifting for system compatibility. Mastery of these concepts is vital for robust C code, despite available APIs for byte swapping.

Link Icon 2 comments
By @zimventures - 4 months
Mildly twitching while reading "build-in" on the meme images.
By @CRConrad - 3 months
Yeah, of course C++ is weird with its array-pointer duality. That's because it's based on C, and that is just as weird with this.

Pascal doesn't have any weird array-pointer duality, so Object Pascal doesn't either.

Feels pretty much like all those L33t K3wl H4xx0rs from the 1970s who claimed that "Real Programmers Don't Use Pascal"[1] were wrong, and St. Niklaus is having the last laugh. Muahahahaa!

___

[1] And all their latter-day followers who never read Bruce Feirstein's Real Men Don't Eat Quiche (and most of whom don't even know what quiche is), but spout off about "quiche-eaters" anyway -- yeah, I'm looking at you, Primeagen... Sigh.