Live data from Hacker News

Why does calloc exist?

vorpus.org

101–110 of 141 posts

Re: Why does calloc exist?

#101
post #92
post #33

Earlier quoted context omitted.

You haven't proven it wrong. Here's the earliest implementation in that repo (in Research UNIX V6; your link in V7): https://github.com/dspinellis/unix-history-repo/blob/Researc... calloc(n, s) { return(alloc(n*s)); } There are several interesting things we learn from poking around V6 though: - `calloc` originated not on UNIX, but as part of Mike Lesk's "iolib", which was written to make it easier to write C programs…

So the (slightly modified) question still stands: Why do calloc and malloc exist? Indeed it looks like calloc was originally intended as a portable way to allocate memory. It used the function alloc which apparently was not meant to be used directly; most iolib functions have a 'c' tacked on. So when iolib was reworked into the stdlib why was calloc kept? saretired suspects backward compatibility but I don't believe…

dmr wrote the V7 malloc and calloc (which calls malloc) [0,1]. Mike Lesk used calloc in lex ;-) [2]

[0] https://github.com/dspinellis/unix-history-repo/blob/Researc...

[1] https://github.com/dspinellis/unix-history-repo/blob/Researc...

[2] https://github.com/dspinellis/unix-history-repo/blob/Researc...

Re: Why does calloc exist?

#102
post #92

Earlier quoted context omitted.

So the (slightly modified) question still stands: Why do calloc and malloc exist? Indeed it looks like calloc was originally intended as a portable way to allocate memory. It used the function alloc which apparently was not meant to be used directly; most iolib functions have a 'c' tacked on. So when iolib was reworked into the stdlib why was calloc kept? saretired suspects backward compatibility but I don't believe…

dmr wrote the V7 malloc and calloc (which calls malloc) [0,1]. Mike Lesk used calloc in lex ;-) [2] [0] https://github.com/dspinellis/unix-history-repo/blob/Researc... [1] https://github.com/dspinellis/unix-history-repo/blob/Researc... [2] https://github.com/dspinellis/unix-history-repo/blob/Researc...

Why are you so sure it was written by dmr? The coding style looks like ken's to me: a) no space after if/while/for/etc b) use of "botch".

Yes, calloc is used in lex, but that is not part of v6...at least not the official distribution, I don't know when he started development. But since he also uses fopen and friends why shouldn't he be using malloc as well? changing 'calloc(n, m)' to 'malloc(n*m)' doesn't sound like such a huge change.

Re: Why does calloc exist?

#103
post #30

Sorry, but this is just goofy and bad. If you depend on copy-on-write functionality, then you need to use an API that is specced to guarantee copy-on-write functionality. If that means you use an #ifdef per platform and do OS-specific stuff, then that is what you do. Anything else is amateur hour. If copy-on-write is a desirable feature, then as the API creator, your job is to expose this functionality in the cleares…

>Anything else is amateur hour. Such as writing the optimizing compilers that make it feasible for you to use C at all?

Is C now so bloated that it needs optimizing compilers just to get executed at all? Is a C interpreter is unfeasable? Apparently even an unoptimizing compiler is not enough.

Re: Why does calloc exist?

#104
post #12

> Plus, if we wanted to, we could certainly write our own wrapper for malloc that took two arguments and multiplied them together with overflow checking. And in fact if we want an overflow-safe version of realloc, or if we don't want the memory to be zero-initialized, then... we still have to do that. Like reallocarray(3) does? buf = malloc(x * y); // becomes buf = reallocarray(NULL, x, y); newbuf = realloc(buf, (x *…

reallocarray(3) looks nifty, but until it's available on a wider, ideally more standard-driven basis than just OpenBSD and FreeBSD, it's likely to not see wide uptake.

reallocarray(3) is part of the portable OpenBSD compat layer[0], and clocks up at under 10 lines on top of realloc(3):

    #define MUL_NO_OVERFLOW ((size_t)1 = MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
             nmemb > 0 && SIZE_MAX / nmemb 
[0] https://github.com/openssh/openssh-portable/blob/master/open...

Re: Why does calloc exist?

#105

I don't get it. The two behaviors are completely orthogonal. Why can't I have a malloc() that does lazy copy-on-write for large arrays and why can't I have an error checking malloc() and why can't I have a calloc() that allocates the memory up front and doesn't zero it out? I get the "it's historic" argument, but this seems like a silly distinction. Sounds like what you want to do practically is basically just make y…

> Why can't I have a malloc() that does lazy copy-on-write for large arrays

reallocarray(3) (though fundamentally that's what malloc does as well, it just doesn't do overflow checking)

> why can't I have an error checking malloc()

reallocarray(3) (because malloc doesn't get the information, it gets a single size rather than a count and a per-object size)

> why can't I have a calloc() that allocates the memory up front and doesn't zero it out?

reallocarray(3)

> Sounds like what you want to do practically is basically just make your malloc() wrap a calloc() with size 1, and stop explicitly memset()ing.

If you're going to memset(0) it there's no reason to use malloc() ever, but it's very common to malloc() then fill the allocation directly, in which case the memsetting is a redundant cost.

Re: Why does calloc exist?

#106

Earlier quoted context omitted.

It's in the standard library, not in the operating system.

The standard library is usually delivered as part of the operating system on *nix-like platforms.

In fact the standard library is usually part of the operating system in the sense that it's the interface to the OS, on both nix-line and non-nix like systems. Linux is the exception (in that raw syscalls are officially supported) not the rule.

Re: Why does calloc exist?

#107
> I mean, let's be honest: if we really cared about security we wouldn't be writing in C.

How so? C is low level, so to be secure you must be fully aware of the behaviours and side effects of what you're doing. In another, perhaps higher level language, sure, there may be less of these gotchas but to be properly secure you need a similar amount of knowledge about background behaviour.

Re: Why does calloc exist?

#109

I you calloc some memory and then the first thing you do is write to it, can the compiler optimize away the initial write of zeros since they will just be overwritten?

To give you a definite answer: Yes.

If you have a calloc, even along conditional paths, it will understand the value along that path is zero until the store, and that the zero is killed by the store.

What it will do varies, because it does not want to screw up the sparse memory usage.

But, for example, it even understands how to take the zero values + stores and turn them into a memset and kill the stores, etc.

It generally does not do something like split the allocation into a calloc and malloc+memset part, or whatever

Post reply on HN