Earlier quoted context omitted.
I think there's a big difference between "why does ... exist" and "why does ... still exist". calloc() may be useful today for reasons completely different from why it existed in the first place. And the difference #2 is just an implementation-specific optimisation. There's nothing in the standard that forces calloc to use lazy allocation / virtual memory. Actually, it may be implemented on platforms which can't prov…
Thank you for bringing up the implementation-specific nature of #2! If the author is running Linux, then perhaps they've never checked out overcommit vs. not overcommit.
Why does calloc exist?
111–120 of 141 posts
Re: Why does calloc exist?
#112I've always been surprised that memset is usually just a nonmagical for loop. I used to expect that the OS does things to magically make it faster (running lazily, etc).
glibc's is all in assembly full of SIMD instructions, which seem very much magical...
https://sourceware.org/git/?p=glibc.git;a=tree;f=sysdeps/x86...
http://stackoverflow.com/questions/8858778/why-are-complicat...
Re: Why does calloc exist?
#113It's harder to forget to multiply by sizeof(T) when calloc-ing as well.
Re: Why does calloc exist?
#114"(I mean, let's be honest: if we really cared about security we wouldn't be writing in C.) " Why so ?
(i.e., don't use a professional-grade band-saw, if you're not a professional)
Re: Why does calloc exist?
#115Earlier quoted context omitted.
Spoken like someone who does not ship fast software!
There's a saying that is often misused, but applies here: "premature optimization is a root of all evil" You first write your code using standard system functions, using the right calls for what you're doing. If after that performance of the code is bad because of calloc() only then you roll out your own implementation (most likely in assembly), and accept that in the future your code might not work well, because som…
Re: Why does calloc exist?
#116Re: Why does calloc exist?
#117Earlier quoted context omitted.
>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.
FYI optimizing compilers are also used to produce efficient instruction streams.
Re: Why does calloc exist?
#118buf = calloc(huge, huge); if (errno) perror("calloc failed"); printf("calloc(huge, huge) returned: %p\n", buf); free(buf); This has a flaw. errno doesn't magically get reset to zero. You should check the return value of calloc, then use errno. Checking if(errno) is not the right way to determine if there was an error.
Its [errno's] value is significant only when the return value of the call indicated an error (i.e., -1 from most calls; -1 or NULL from most library functions); a function that succeeds is allowed to change errno.
Re: Why does calloc exist?
#119Re: Why does calloc exist?
#120Sorry, 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…
I'm agreeing with you here - if your intent is to have copy-on-write functionality, that's not what you're communicating when you use calloc.
It's okay for an implementation to try to optimize given the constraints of intent, but I agree that if an implementation is doing something non-straightforward (copy-on-write in this case), that is a smell that perhaps the API needs to expose an additional layer.