On 11/17/22 03:16, Amit wrote:
> On Thursday, November 17, 2022 at 1:14:49 PM UTC+5:30,
>
james...@alumni.caltech.edu wrote:
>> On 11/17/22 01:20, Amit wrote:
>>> Hi,
>>> I prefer long over size_t.
>> For which purpose? C supports a large number of different types,
>> precisely because different types are preferred for different
>> purposes. More often than not, the type I need to use is dictated by
>> the interfaces to standard and third-part libraries that I'm using.
>> When I do have a choice in the matter, I prefer unsigned types like
>> size_t in any context where bitwise operations are involved, because
>> many of those operations can have undefined behavior under certain
>> circumstances when using a signed types, but not with unsigned types.
>> Unsigned types are often preferred when the quantity in question is
>> incapable of being negative, though care must be used when applying
>> this criterion. Operations involving quantities that cannot be
>> negative, may have results that can be negative. If such operations
>> are relevant to a particular variable, that variable should have a
>> signed type.
>
> I am not talking about signed vs unsigned.
True, but signed vs. unsigned is the most important difference between
long and size_t that would affect decisions about which one to use. The
only other difference is that long is guaranteed to be able to hold any
value between -
2147483648 and
2147483647, which requires at least 32
bits, while size_t is only required to have at least 16 bits (and on
some systems is in fact that small).
> What reasons glibc original authors had when they used size_t in these
> functions instead of long.
They wanted to match the existing implementations of malloc(). Keep in
mind that C dates back to the early 70s. It was released to the general
public by the publication of "The C programming language" in 1978. I
started programming in C in 1979. What we now call the C standard
library had already started taking form at that time, though it wasn't
standardized until 1989. I'm not sure when in the process malloc() was
added to that library, but I believe it was well before glibc came out
in the late 1980s.
> If we don't know those reasons then if a developer has to implement a
> function called allocate_memory() then which implementation should he
> use and WHY?
Well, if he's creating an implementation of C, he should use the
interface that has been specified for malloc().
If you were instead to ask the people who designed malloc() chose
size_t, I would guess that it's because it is never valid to request a
negative amount, so they chose an interface that guarantees that it will
never receive a negative amount. It's also never valid for the size of a
type to negative, which is why the value of a sizeof expression has the
type size_t. In most cases, the value which should be passed to a
malloc() call should be the result of a calculation involving size_t.
Because of the way C's usual arithmetic conversions work, there's a very
good chance that any such calculated value has, itself, a type of
size_t, which is how it all fits together.