7 ms·
Any idea why they would use long long rather than int64_t?
by alextingle 13y ago
Any idea why they would use long long rather than int64_t?
- sirclueless 13y agoint64_t is a userspace libc typedef. long long is ICO C (C99) and guaranteed to be 64 bits or more.
- alextingle 13y agoSo stdint.h not available to kernel programmers. Thanks.
- pslam 13y agoI always found this special-casing of headers for kernel vs user unnecessary and complicating. In the many embedded runtimes I maintain, I've moved towards using standard POSIX-y and C(++) standard headers everywhere. I know kernel folks love to believe that their world is special so all the usual C library stuff needs to be done differently, but it's not needed. It's so much easier to port code between the two worlds when you don't have to litter the #include prelude of every C file with conditionals.
- _kst_ 13y agostdint.h may not be available, but int64_t is; it's defined in include/linux/types.h.
- alayne 13y agoThe OpenBSD kernel probably doesn't use Linux headers.
- asveikau 13y agoA few times I got flamed on this site for complaining that the average HNer can't have reasonable discussions about C. I think the comment that the OpenBSD team should just #include <linux/types.h> pretty well shows that I was right. :-)
- _kst_ 13y agoJust a silly mistake on my part.
- cbsmith 13y agoReally going out on a limb there aren't you? ;-)
- _kst_ 13y agoYeah, good point. I'm not familiar with the OpenBSD kernel. Is there any good reason (for OpenBSD or any other kernel) why <stdint.h> shouldn't be available -- or at least why int64_t shouldn't be available in some header?
- _kst_ 13y ago<time.h> is userspace. It's at least theoretically possible for long long to be more than 64 bits. Why use long long (potentially, say, 128 bits) rather than int64_t for time_t? And why wouldn't int64_t be available in kernel space?
- dmm 13y agolong long can be printed in printf with the "%lld" flag.
- mrb 13y agoint64_t is in the ICO C(C99) standard: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/n869.pdf.gz http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/n869.pd...
- tedunangst 13y agoHow do you think the int64_t type is constructed? Few problems are solved by endless stacks of typedefs.
- Peaker 13y agoint64_t is a much nicer type than "long long". "int" makes some sense. Word-size of the machine. "short", "long", "long long" are all non-sensical. You use them when you want to trade size and range. When you want to make that trade-off, you care what their sizes are. Instead of lower/upper bounds on their sizes, which aren't very useful, they should just have specific sizes. At which point, you might as well use uint32_t, and uint64_t in place of long and long long. Prefer the sized int types over the "long"/"long long" ones when you can, for saner coding. Use uintptr_t and such when you need a ptr-sized int, rather than a specific size.
- tedunangst 13y agoonce again, how do the headers conjure up the int64_t type? it's not a keyword.
- cmccabe 13y agoHere is how stdint.h on Linux conjures up the int64_t type. #if __WORDSIZE == 64 typedef long int int64_t; #else typedef long long int int64_t; #endif
- tedunangst 13y agoWait. You just typed long long. You're not allowed to do that.
- Peaker 13y agoYou're whooshing. You don't want to use "long long" because that's not necessarily 64-bits. You want to use int64_t which guarantees it is 64-bits. And then, the correct format specifier for that is PRIi64, and not "%ld" or "%lld" which will break in different platforms.
- dmm 13y agoImagine you just called time(2) and got a time_t value. Something like: time_t asdf = time(); And now you want to use printf to print that value to the screen. You can cast to 'long long', which is guaranteed to be at at least 64 bits wide, and ensure no loss of precision occurs: printf("%lld", (long long)asdf); That will work whether time_t is 32 bit or 64 bit.
- Peaker 13y agoIf they just added a macro to inttypes.h, you could use that for the format specifier. If you want to print a uint32_t, you just use PRIu32.