Calloc arguments order

B

Ben Bacarisse

Eric Sosman said:
On 8/19/2012 4:55 PM, Raj Pashwar wrote:

None. Well, at any rate "very little."
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,8) 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>
 
E

Eric Sosman

Eric Sosman said:
On 8/19/2012 4:55 PM, Raj Pashwar wrote:

None. Well, at any rate "very little."
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,8) 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. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,078
Messages
2,570,570
Members
47,204
Latest member
MalorieSte

Latest Threads

Top