Polish API unsigned long vs. size_t for sizes/lengths #175

Open
opened 2017-03-23 02:54:40 -05:00 by karel-m · 5 comments
karel-m commented 2017-03-23 02:54:40 -05:00 (Migrated from github.com)

As mentioned in #174 and #173 we should consider switching from unsigned long to size_t when passing the length or size of something.

@rofl0r said:

also: in C the proper type for sizes is size_t, not unsigned long. (usually it's the same, but some odd platforms like windows have a 32bit long on 64bit).

and @rofl0r a bit later:

using size_t does not necessarily mean including stdint.h. since you're a fan of ifdef hacks over configure tests you could hardcode a list of CPP defines which define HAVE_STDINT_H and only use it there, for the other platforms fall back to typedefing it to unsigned long, or unsigned long long on windows64 bit.

As mentioned in #174 and #173 we should consider switching from `unsigned long` to `size_t` when passing the length or size of something. @rofl0r said: > also: in C the proper type for sizes is size_t, not unsigned long. (usually it's the same, but some odd platforms like windows have a 32bit long on 64bit). and @rofl0r a bit later: > using size_t does not necessarily mean including stdint.h. since you're a fan of ifdef hacks over configure tests you could hardcode a list of CPP defines which define HAVE_STDINT_H and only use it there, for the other platforms fall back to typedefing it to unsigned long, or unsigned long long on windows64 bit.
rofl0r commented 2017-03-23 10:26:25 -05:00 (Migrated from github.com)

i thought about this some more and i came to the conclusion that in order to keep future problems to a mininum it's probably not a good idea to make a manual typedef to size_t on platforms that currently do not provide stdint.h, because future version may actually have stdint.h and pull in the header implicitly through inclusion of another header, so the typedef will be problematical. so if you want to go that route it's preferable to have a custom type, for example ltc_size, which is typedef'd to unsigned long or size_t, depending on the platform.

i thought about this some more and i came to the conclusion that in order to keep future problems to a mininum it's probably not a good idea to make a manual typedef to size_t on platforms that currently do not provide stdint.h, because future version may actually have stdint.h and pull in the header implicitly through inclusion of another header, so the typedef will be problematical. so if you want to go that route it's preferable to have a custom type, for example ltc_size, which is typedef'd to unsigned long or size_t, depending on the platform.
karel-m commented 2017-03-23 14:10:19 -05:00 (Migrated from github.com)

To my surprise even pretty old MS compilers are able to handle size_t.

To my surprise even pretty old MS compilers are able to handle `size_t`.
sjaeckel commented 2017-03-23 17:46:25 -05:00 (Migrated from github.com)

TBH I'm not aware of any platform that I used lately which doesn't have stdint.h so I'm against introducing something like ltc_size or even worse some "macro-own-typedef-magic"^TM. I'd rather make it up to the integrator of the library to provide a stdint.h himself with the appropriate typedefs if there's none available ootb.

TBH I'm not aware of any platform that I used lately which doesn't have `stdint.h` so I'm against introducing something like `ltc_size` or even worse some "macro-own-typedef-magic"^TM. I'd rather make it up to the integrator of the library to provide a `stdint.h` himself with the appropriate typedefs if there's none available ootb.
rofl0r commented 2017-03-23 18:11:39 -05:00 (Migrated from github.com)

btw according to man 0p stddef.h, stddef.h is the proper header to include to get this type.

btw according to `man 0p stddef.h`, stddef.h is the proper header to include to get this type.
sjaeckel commented 2017-03-23 18:28:48 -05:00 (Migrated from github.com)

stddef.h is the proper header to include to get this type

mea culpa, you're right! no more typing for me today

> stddef.h is the proper header to include to get this type mea culpa, you're right! no more typing for me today
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-libtom/libtomcrypt#175
No description provided.