Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Calloc arguments order

Reply
Thread Tools

Calloc arguments order

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-21-2012
Eric Sosman <(E-Mail Removed)> writes:

> On 8/19/2012 4:55 PM, Raj Pashwar wrote:

<snip>
>> I would like to understand something about the Calloc function.
>>
>> void *calloc(size_t nmemb, size_t size);
>>

<snip>
>> What I don't understand is: how is this any different from allocating
>> memory for an array of size elements of nmemb bytes each? Does the order
>> of arguments make much difference really? What difference does it make
>> having them in one order instead of the other?

>
> None. Well, at any rate "very little."

<snip>
> So, why did the primeval calloc() have two parameters instead
> of just one? I'm only guessing, but my guess is that calloc() may
> have been invented on a system where memory was scarce, and perhaps
> the earliest versions *did* in fact treat the parameters differently.
> Maybe calloc(128,1) said "Oh, 1-byte objects: I don't need to worry
> about alignment" whereas calloc(16, would say "Oh, 8-byte objects:
> they might be `double', so I'd better use strict alignment." Such
> a stratagem (if it existed; remember, I'm just guessing) ceased to
> have meaning when ANSI decreed that all dynamic allocations had to
> be strictly aligned, but things might have been different in the
> Bronze Age.


Well, it's a damn good guess! K&R p157:

"The function calloc is rather like the alloc we have used in
previous chapters.

calloc(n, sizeof(object))

returns a pointer to enough space for n objects of the specified size,
or NULL if the request cannot be satisfied. The storage is
initialized to zero.

*The pointer has the proper alignment* for the object in question,
but it should be cast into the appropriate type, as in

char *calloc();
int *ip;

ip = (int *) calloc(n, sizeof(int));

cfree(p) frees the space pointed to by p, where p was originally
obtained form a call to calloc."

[Historical note: there's no malloc() and no free() at this stage.]

<snip>
--
Ben.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-21-2012
On 8/20/2012 8:55 PM, Ben Bacarisse wrote:
> Eric Sosman <(E-Mail Removed)> writes:
>
>> On 8/19/2012 4:55 PM, Raj Pashwar wrote:

> <snip>
>>> I would like to understand something about the Calloc function.
>>>
>>> void *calloc(size_t nmemb, size_t size);
>>>

> <snip>
>>> What I don't understand is: how is this any different from allocating
>>> memory for an array of size elements of nmemb bytes each? Does the order
>>> of arguments make much difference really? What difference does it make
>>> having them in one order instead of the other?

>>
>> None. Well, at any rate "very little."

> <snip>
>> So, why did the primeval calloc() have two parameters instead
>> of just one? I'm only guessing, but my guess is that calloc() may
>> have been invented on a system where memory was scarce, and perhaps
>> the earliest versions *did* in fact treat the parameters differently.
>> Maybe calloc(128,1) said "Oh, 1-byte objects: I don't need to worry
>> about alignment" whereas calloc(16, would say "Oh, 8-byte objects:
>> they might be `double', so I'd better use strict alignment." Such
>> a stratagem (if it existed; remember, I'm just guessing) ceased to
>> have meaning when ANSI decreed that all dynamic allocations had to
>> be strictly aligned, but things might have been different in the
>> Bronze Age.

>
> Well, it's a damn good guess! K&R p157:
>
> "The function calloc is rather like the alloc we have used in
> previous chapters.
>
> calloc(n, sizeof(object))
>
> returns a pointer to enough space for n objects of the specified size,
> or NULL if the request cannot be satisfied. The storage is
> initialized to zero.
>
> *The pointer has the proper alignment* for the object in question,
> but it should be cast into the appropriate type, as in
>
> char *calloc();
> int *ip;
>
> ip = (int *) calloc(n, sizeof(int));
>
> cfree(p) frees the space pointed to by p, where p was originally
> obtained form a call to calloc."
>
> [Historical note: there's no malloc() and no free() at this stage.]


There was, however, cfree() -- A couple lines down, on the
same page. Doesn't say whether it zeroed the memory before
releasing it.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
function call with arguments which takes no arguments Neo C Programming 10 01-20-2005 06:31 AM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM



Advertisments