Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Why does calloc accept 2 arguments, and with what arguments should one call it?

Post

Why does calloc accept 2 arguments, and with what arguments should one call it?

+5
−0

According to the standard (C17 draft, 7.22.3.2) The function calloc

void *calloc(size_t nmemb, size_t size);

"allocates space for an array of nmemb objects, each of whose size is size [and] initialize[s] [...] all bits [to] zero". Like malloc, it returns a void * pointer to the allocated space or a null pointer on failure.

Unlike malloc

void *malloc(size_t nbytes);

calloc takes two arguments. I read that the function signature of calloc lets a good implementation check for some sort of multiplicative overflow. For example, this manpage states (formatting adapted):

If the multiplication of nmemb and size would result in integer overflow, then calloc() returns an error. By contrast, an integer overflow would not be detected in the following call to malloc(), with the result that an incorrectly sized block of memory would be allocated:

malloc(nmemb * size);

But I also heard that its 2-argument function signature is flawed and that the following calls are equivalent:

calloc(1, m*n)

calloc(m, n)

calloc(n, m)

calloc(m*n, 1)

(The last example was added by myself.)

This leads me to ask: Why does calloc accept 2 arguments, and with what arguments should one call it? Is its function signature designed well?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

"Also heard" where, and what exactly? (4 comments)
"Also heard" where, and what exactly?
Karl Knechtel‭ wrote over 1 year ago

It's hard to address the part where you "also heard that its 2-argument function signature is flawed" because we do not know where you heard it, and therefore we cannot possibly know what argument was being presented. This doesn't add anything to the post as it stands; if there is a flaw, then answerers are perfectly capable of raising the point unprompted.

Skipping 1 deleted comment.

Lover of Structure‭ wrote over 1 year ago

I made some edits; feel free to have a look.

Lundin‭ wrote over 1 year ago

"I heard it through the grapevine"? :) This was just recently discussed at SO, I may be to blame for starting that discussion https://stackoverflow.com/a/76945224/584518.

Lover of Structure‭ wrote over 1 year ago · edited over 1 year ago

Since you admitted to it, I should do the same: Knowing that you're active here, I knew you'd see this anyway, plus it's better to have this question not have a dependency on our lovely sister site ;-)