Can I be sure that data conversion always succeeds? If I have an int
and I use mmap(2)
, is the int
converted to off_t
and size_t
? Why are there all those different sizes? And are there conversion function?
Asked
Active
Viewed 790 times
-1

Kouros
- 341
- 5
- 19
-
2You can definitely not be sure that conversions always succeed. For example, conversions from integer types to floating-point types and from floating-point to integer result in undefined behavior if the values cannot be represented in the new type. – EOF Oct 08 '15 at 22:43
-
Can you be sure that conversions always succeed, in general? No. And in your specific example, `off_t` isn't even standard C, it's POSIX. Although `size_t` and `int` are both guaranteed to be at least 16-bits. – Iskar Jarak Oct 08 '15 at 22:47
-
1Also, by _"Why are there all those different sizes?"_, what do you mean? Do you mean why are different types not all the same size, or why are there different types, or...? – Iskar Jarak Oct 08 '15 at 22:49
-
Of interest: https://github.com/git/git/commit/aafa5df0df39036c6500846acd3db5b75d264a3b – VonC Jun 11 '21 at 20:56
1 Answers
2
I am not sure about the usage of the mmap function you are doing since mmap returns a pointer.
Regarding conversion you can basically convert any variable to anything using a cast, however it is your responsibility to verify that this will work for example:
int i; double d; d = 42.3; i = (int)d; printf("i = %d", i);
Output will be 42, without the cast (int)
some compilers would probably complain and warn you that you will lose floating precision.
- About the why of all these different sizes, first thing
size_t
is meant to represent a size so you can grossly think of it as anunsigned int
, regarding why not use aunsigned int
rather than a typedef'ed "size_t", see this post unsigned int vs size_t.

Community
- 1
- 1

matanwrites
- 812
- 1
- 10
- 18