Portability Issues in C/C++
Portability Issues in C/C++
1. Data Type Sizes
The Problem
Integer and pointer sizes vary across different platforms.
| |
2. Endianness
The Problem
Different CPUs store multi-byte values in different ways.
| |
3. Compiler-Specific Extensions
| |
4. Operating System Differences
File Paths
| |
System APIs
| |
5. Character Sets and Encodings
| |
6. Struct Padding and Alignment
| |
7. Pointer Issues
| |
8. Signed vs Unsigned
| |
9. Undefined and Unspecified Behavior
| |
10. Floating-point Portability
| |
11. Line Endings
| |
12. Preprocessor Differences
| |
13. Standard Library Variants
| |
14. Integer Promotion and Conversion
| |
15. Calling Conventions
| |
Best Practices for Portable Code
1. Use Standard Headers
| |
2. Feature Testing
| |
3. Calculate Sizes with sizeof
| |
4. Configuration Headers
| |
5. Cross-Platform Libraries
- Use libraries like Boost (C++), GLib, APR, etc.
- Abstract OS-specific functionality.
- Use CMake for build portability.
Key Takeaway: Write code that conforms to standards (C99/C11/C++11/14/17/20), avoid assumptions about implementation details, test on multiple platforms, and use abstraction layers for OS-specific features.
Last updated on