0

I just ran into a smallish issue when working with size_t values in some code.

First I wanted to use size_t because it's [guaranteed] to be the same size on most platforms, that way going from 32 to 64 bit shouldn't cause any issues.

Then I negated the size_t value and got a really big number, looking back I realized that size_t is unsigned. There is intptr_t which is also signed but should be used for pointers and there's uintptr_t which is unsigned and also used for pointers.

My eventual goal is to implement a simple set of fixed point math functions in c.

My question is can I use uintptr_t like an int in all my calculations? Could that cause any issues?

user1610950
  • 1,837
  • 5
  • 33
  • 49
  • Have you tried `off_t`? – 0xC0000022L Jul 21 '15 at 08:06
  • 1
    size_t on linux/gcc/32bit == 32bit, on linux/gcc/64bit == 64bit – fghj Jul 21 '15 at 08:10
  • What's the use case of your library? What guarantees do you want to give regarding the range of values? – Werner Henze Jul 21 '15 at 08:10
  • the use case is unit quaternions and vector3 and quaternions to 4x4 matrix. Nothing more for this part of the code. Since quaternions and radians are more fraction part heavy i'd like to have more bits for the fractional part. Also degrees in radians have can fit in a size up to 2pi or a modulus of it. – user1610950 Jul 21 '15 at 08:17
  • Take a look at the "checked as answered" answer of [THIS](http://stackoverflow.com/questions/1845482/what-is-uintptr-t-data-type) post. – LPs Jul 21 '15 at 08:17
  • Why does it have to work the same on all platforms? Your problem is naturally bounded by the platform limitations, so why can't you just make the program work according to each platform's limitations, rather than try to come up with some universal, yet still arbitrary constants? – Kerrek SB Jul 21 '15 at 08:17
  • I was just testing (int)size_t returns the correct value but I get the feeling this will cause trouble. @KerrekSB I would just like something that's written cleanly. Are there no signed values similar to size_t but signed? I don't feel comfortable down casting a size_t to a signed int. – user1610950 Jul 21 '15 at 08:29
  • Posix has `ssize_t`; in C++ you can use type traits to derive the matching signed type. In pure standard C... no. – Kerrek SB Jul 21 '15 at 08:43
  • 1
    The only negative value `ssize_t` is guaranteed to be able to store is `-1`. – alk Jul 21 '15 at 08:56

2 Answers2

6

If you want to have exactly the same number of bits on all platforms, simply use for instance int32_t as defined in stdint.h

marcolz
  • 2,880
  • 2
  • 23
  • 28
1

You could consider ptrdiff_t which is the type that a difference of pointers produces. On all common platforms this should be the signed type corresponding to size_t.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • after doing some more research it seems that ptrdiff_t is signed and should be able to accomplish what I need. Let's hope this choice doesn't turn into bugs down the road. – user1610950 Jul 21 '15 at 14:36
  • That is pretty sure to be of different size on 32 bit systems and 64 bit systems. On Linux: `cat ptrdiff_t.c; cc ptrdiff_t.c -o ptrdiff_t; ./ptrdiff_t; cc -m32 ptrdiff_t.c -o ptrdiff_t; ./ptrdiff_t: #include #include int main() { printf("%zu\n", sizeof(ptrdiff_t)); return 0; } 8 4` – marcolz Jul 21 '15 at 15:43