unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* size_t vs long.
@ 2022-11-17  7:02 A via Libc-alpha
  2022-11-17  9:21 ` Alejandro Colomar via Libc-alpha
  2022-11-17 21:58 ` DJ Delorie via Libc-alpha
  0 siblings, 2 replies; 15+ messages in thread
From: A via Libc-alpha @ 2022-11-17  7:02 UTC (permalink / raw)
  To: libc-alpha

Hi,

I prefer long over size_t.

This is because, in case the user passes a negative number by mistake
then I will be able to check it if the type is long and return
immediately.

But if size_t is used, then most probably, it will result in a crash -
like malloc(-1) will crash the program because unsigned -1 is
0xFFFFFFFF and this much memory is not available on today's computers
and probably may not be available at all in future also (RAM size of
2^64 bits is really really huge).

Another thing is that if size_t is used an array index then array[-1]
will result in wrong behavior or program crash. But with long, the
developer can check whether the index is negative, thus avoiding
program crash.

So, in my opinion, long should be used instead of size_t.

I know that original glibc authors had chosen size_t, so there must be
some reason for that, however that reason is not clear to me.

Amit

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-11-23 20:08 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-17  7:02 size_t vs long A via Libc-alpha
2022-11-17  9:21 ` Alejandro Colomar via Libc-alpha
2022-11-17  9:48   ` A via Libc-alpha
2022-11-17 11:00     ` Alejandro Colomar via Libc-alpha
2022-11-17 19:40       ` Jason Duerstock via Libc-alpha
2022-11-17 20:01         ` Alejandro Colomar via Libc-alpha
2022-11-17 19:17   ` Paul Eggert
2022-11-17 20:27     ` Alejandro Colomar via Libc-alpha
2022-11-17 21:39       ` Paul Eggert
2022-11-17 23:04         ` Alejandro Colomar via Libc-alpha
2022-11-23 20:08           ` Using size_t to crash on off-by-one errors (was: size_t vs long.) Alejandro Colomar via Libc-alpha
2022-11-18  2:11         ` size_t vs long Maciej W. Rozycki
2022-11-18  2:47           ` Paul Eggert
2022-11-23 20:01             ` Alejandro Colomar via Libc-alpha
2022-11-17 21:58 ` DJ Delorie via Libc-alpha

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).