Last active
July 18, 2025 05:39
-
-
Save scivision/85eb64a6f0fcb0b3bcbb965229486428 to your computer and use it in GitHub Desktop.
Use ssize_t on POSIX systems and with MSVC compilers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <sys/types.h> | |
| #ifndef ssize_t | |
| #ifdef _WIN32 | |
| #include <BaseTsd.h> | |
| #define ssize_t SSIZE_T | |
| #else | |
| #define ssize_t ptrdiff_t | |
| #endif | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdint.h> /* C23: SIZE_WIDTH, PTRDIFF_WIDTH */ | |
| #include <stddef.h> | |
| #include <stdio.h> | |
| #include <sys/types.h> | |
| #include "mysize.h" | |
| int main(void){ | |
| size_t s; | |
| ssize_t t; | |
| #ifdef SIZE_WIDTH | |
| printf("SIZE_WIDTH: %d PTRDIFF_WIDTH %d\n", SIZE_WIDTH, PTRDIFF_WIDTH); | |
| #endif | |
| printf("sizeof(size_t): %zu sizeof(ssize_t): %zu\n", sizeof(s), sizeof(t)); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdint> // C23: SIZE_WIDTH, PTRDIFF_WIDTH | |
| #include <cstddef> // std::size_t | |
| #include <bit> // std::bit_width | |
| #include <iostream> | |
| #include <sys/types.h> | |
| #include "mysize.h" | |
| int main() | |
| { | |
| size_t s; | |
| ssize_t t; | |
| #ifdef SIZE_WIDTH | |
| std::cout << "SIZE_WIDTH: " << SIZE_WIDTH << " PTRDIFF_WIDTH: " << PTRDIFF_WIDTH << std::endl; | |
| #endif | |
| #ifdef __cpp_lib_int_pow2 | |
| std::cout << "std::bit_width(size_t): " << std::bit_width(SIZE_WIDTH) << " std::bit_width(ssize_t): " << std::bit_width(PTRDIFF_WIDTH) << std::endl; | |
| #endif | |
| std::cout <<"sizeof(size_t): " << sizeof(s) << " sizeof(ssize_t): " << sizeof(t) << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment