Why does calloc exist?
vorpus.org
Why does calloc exist?
1–10 of 141 posts
Re: Why does calloc exist?
#2On the flip side, if your critical metric is latency then these tricks of calloc's and the OS's are exactly what you try to avoid. memset() the buffer, and if you have the privileges you should mlock() it to prevent it from being paged out. Of course, this all presumes that it's not an ephemeral buffer to begin with. Best to change your design to leverage a long-lived resource if possible.
Re: Why does calloc exist?
#3Here's an early implementation: https://github.com/dspinellis/unix-history-repo/blob/Researc...
Re: Why does calloc exist?
#4Re: Why does calloc exist?
#5That's a nice alternative history fiction. Here's an early implementation: https://github.com/dspinellis/unix-history-repo/blob/Researc...
Re: Why does calloc exist?
#6> So basically, calloc exists because it lets the memory allocator and kernel engage in a sneaky conspiracy to make your code faster and use less memory. You should let it! Don't use malloc+memset! On the flip side, if your critical metric is latency then these tricks of calloc's and the OS's are exactly what you try to avoid. memset() the buffer, and if you have the privileges you should mlock() it to prevent it fro…
Best to change your design to leverage a long-lived
resource if possible.
On the flip side, if your critical metric is latency
then these tricks [...] are exactly what you try to avoid
If you keep the buffer alive as long as possible with a slab allocator, or just smart/good memory management strategy. How you acquire the buffer will ultimately be trivial, likely dwarfed your other startup tasks (reading config, opening sockets, etc.)Re: Why does calloc exist?
#7Re: Why does calloc exist?
#8I always thought it was because of padding. An array of M structures each N bytes long could require more than M*N bytes (certainly has on some architectures I've worked with). But I guess that's not it after all.
(Despite the syntax, the same goes for operator new in C++! Placement vector new in particular is a trap.)
Re: Why does calloc exist?
#9I always thought it was because of padding. An array of M structures each N bytes long could require more than M*N bytes (certainly has on some architectures I've worked with). But I guess that's not it after all.
Re: Why does calloc exist?
#10I always thought it was because of padding. An array of M structures each N bytes long could require more than M*N bytes (certainly has on some architectures I've worked with). But I guess that's not it after all.
struct S {
long a;
char b;
};
On my computer (64-bit Mac), sizeof(struct S) is 16, due to 7 bytes of padding after b. Since the compiler handles the padding, that means calloc doesn't have to.