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.

Post History

77%
+5 −0
Q&A Why does calloc accept 2 arguments, and with what arguments should one call it?

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] i...

1 answer  ·  posted 1y ago by Lover of Structure‭  ·  last activity 1y ago by Lundin‭

#4: Post edited by user avatar Lover of Structure‭ · 2023-08-22T05:27:14Z (about 1 year ago)
formatting (very minor)
  • 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](https://manned.org/calloc.3) 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?**
  • 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](https://manned.org/calloc.3) 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?**
#3: Post edited by user avatar Lover of Structure‭ · 2023-08-22T03:50:35Z (about 1 year ago)
added one more example
  • 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](https://manned.org/calloc.3) 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(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?**
  • 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](https://manned.org/calloc.3) 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?**
#2: Post edited by user avatar Lover of Structure‭ · 2023-08-22T03:31:05Z (about 1 year ago)
made question more specific in two aspects
  • 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 heard that the function signature of `calloc` lets a good implementation check for some sort of multiplicative overflow. But I also heard that its 2-argument function signature is flawed and that one should call it as `calloc(1, ...)`. 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?**
  • 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](https://manned.org/calloc.3) 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(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?**
#1: Initial revision by user avatar Lover of Structure‭ · 2023-08-21T18:11:43Z (about 1 year ago)
Why does calloc accept 2 arguments, and with what arguments should one call it?
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 heard that the function signature of `calloc` lets a good implementation check for some sort of multiplicative overflow. But I also heard that its 2-argument function signature is flawed and that one should call it as `calloc(1, ...)`. 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?**