From: DJ Delorie <dj@redhat.com>
To: A <amit234234234234@gmail.com>
Cc: libc-alpha@sourceware.org
Subject: Re: size_t vs long.
Date: Thu, 17 Nov 2022 16:58:23 -0500 [thread overview]
Message-ID: <xna64p8cqo.fsf@greed.delorie.com> (raw)
In-Reply-To: <CAOM0=dac1uM+96yT7f_GDp1f0bF=oW2JoDPFPPOgd4OjzRR+mA@mail.gmail.com>
A via Libc-alpha <libc-alpha@sourceware.org> writes:
> I prefer long over size_t.
On many platforms, long and size_t are not the same size (in bits). On
many 16 bit platforms, long is 32 bits and size_t (and all pointers) are
16. On some platforms, long is only 32 bits and size_t is 64. In the
X32 ABI long is 64 bits but size_t is only 32. It's because these cases
exist that there are separate types for numbers vs pointers vs sizes.
> 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.
You want ssize_t then. It's the same size as size_t, but signed. For
math on pointers, use ptrdiff_t. There's even an intptr_t (and
uintptr_t) that is whatever integer type is the same size as a pointer.
> 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
malloc should not accept a size greater than half the address space (as
per the spec) so it won't matter if the parameter is signed or unsigned.
prev parent reply other threads:[~2022-11-17 21:58 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-17 7:02 A
2022-11-17 9:21 ` Alejandro Colomar
2022-11-17 9:48 ` A
2022-11-17 11:00 ` Alejandro Colomar
2022-11-17 19:40 ` Jason Duerstock
2022-11-17 20:01 ` Alejandro Colomar
2022-11-17 19:17 ` Paul Eggert
2022-11-17 20:27 ` Alejandro Colomar
2022-11-17 21:39 ` Paul Eggert
2022-11-17 23:04 ` Alejandro Colomar
2022-11-23 20:08 ` Using size_t to crash on off-by-one errors (was: size_t vs long.) Alejandro Colomar
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
2022-11-17 21:58 ` DJ Delorie [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xna64p8cqo.fsf@greed.delorie.com \
--to=dj@redhat.com \
--cc=amit234234234234@gmail.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).