Oh, that makes sense.
Speaking of clever tricks, would it be faster or slower to use c99's variable-length arrays instead of malloc?
Speaking of clever tricks, would it be faster or slower to use c99's variable-length arrays instead of malloc?
But a single malloc is nothing compared to the cost of a context switch into kernel mode.
#define BUF_INITIAL 1024
char buf[BUF_INITIAL];
int main(int argc, char** argv)
{
char* p;
...
p = len > BUF_INITIAL ? malloc(len) : buf;
...
write(1, p, len);
...
}4 KB * (many) could be frightening.
I'm also guessing that 4k was chosen because a malloc() of 1 page is faster than a malloc() of >1 page. Of course that's with the assumption that the systems use a 4k page size.