Live data from Hacker News

Why does calloc exist?

vorpus.org

121–130 of 141 posts

Re: Why does calloc exist?

#121

Somebody needs to go update all the StackOverflow answers saying that malloc is faster. According to this, calloc seem to always be faster with several other benefits as well.

It is not as you say.

The article suggests that Malloc + Memset is slower than Calloc.

Malloc will be faster depending on your use case. If your plan is to eventually call memset. Then just use Calloc, otherwise malloc will be faster all the time.

Re: Why does calloc exist?

#123

Earlier quoted context omitted.

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.

Yes, exactly. This is especially true on Darwin and Solaris.

Re: Why does calloc exist?

#124

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.

Making statements as inaccurate as possible without being wrong is a fun game, I guess.

My turn: A file explorer is part of the OS because it is installed by default on Windows. A good OS provides a graphical file browser.

Re: Why does calloc exist?

#125
post #115
post #90

Earlier quoted context omitted.

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…

It's not always the best way to write software. If you're writing a program that's supposed to work in real(ish) time then it's good to take performance into consideration early on, otherwise you'll end up rearchitecting your program later. It's not necessarily about a number of cycles each operation takes, but rather about memory layout of your data. I guess it's a matter of experience: if you expect something to be…

That's why I mean when I said that the saying is abused. Some people think that choosing the right algorithm is premature optimization. It is not.

Choosing whether to use malloc vs calloc is not an architectural change though, and in fact it is very easy to replace one with the other, but if you use the right call for right use case, then you will benefit from optimizations that the OS provides, and often you might not even be able to achieve them from user space.

Re: Why does calloc exist?

#126

Somebody needs to go update all the StackOverflow answers saying that malloc is faster. According to this, calloc seem to always be faster with several other benefits as well.

It is not as you say. The article suggests that Malloc + Memset is slower than Calloc. Malloc will be faster depending on your use case. If your plan is to eventually call memset. Then just use Calloc, otherwise malloc will be faster all the time.

n00b question: Why would you not memset? I would assume you'd want to start with all zeroed memory in almost all cases.

Re: Why does calloc exist?

#127
post #77
post #61

Earlier quoted context omitted.

The output of a modern C compiler is unpredictable in terms of performance. Yet mannykannot suspects you still use such tools. Please explain your inconsistency.

Even if this weren't a fallacious argument, he's actually in the process of replacing Thekla's C/C++ workflow with a custom language called Jai.

Isn't Jai piggybacking on C?

Re: Why does calloc exist?

#128
post #77

Earlier quoted context omitted.

Even if this weren't a fallacious argument, he's actually in the process of replacing Thekla's C/C++ workflow with a custom language called Jai.

Isn't Jai piggybacking on C?

He started with two backends. One generates bytecode for an internal interpreter, and this is still needed because all Jai code can be run at compile time. The other backend generates C code, but it's a temporary measure. He just added the LLVM backend: https://www.youtube.com/watch?v=HLk4eiGUic8

Re: Why does calloc exist?

#129
post #128

Earlier quoted context omitted.

Isn't Jai piggybacking on C?

He started with two backends. One generates bytecode for an internal interpreter, and this is still needed because all Jai code can be run at compile time. The other backend generates C code, but it's a temporary measure. He just added the LLVM backend: https://www.youtube.com/watch?v=HLk4eiGUic8

oh thanks, I didn't see the last one :)

Re: Why does calloc exist?

#130

Earlier quoted context omitted.

It is not as you say. The article suggests that Malloc + Memset is slower than Calloc. Malloc will be faster depending on your use case. If your plan is to eventually call memset. Then just use Calloc, otherwise malloc will be faster all the time.

n00b question: Why would you not memset? I would assume you'd want to start with all zeroed memory in almost all cases.

Not always, you could be planning on filling the data with something else. Very common to do that.
Post reply on HN