** help...

Christopher R Volpe volpe at underdog.crd.ge.com
Wed Aug 22 22:18:58 AEST 1990


In article <1884 at jura.tcom.stc.co.uk>, rmj at tcom.stc.co.uk (Rhodri James)
writes:
|>You want the whole list or just the short version? :-)
|>
|>1) array is an int **  so the malloc should be cast to that
|>2) the "elements" are int *, so that should be what you take sizeof.
|>Pointers haven't been equivalent to ints for some while.
|>3) this isn't vastly relevant to PC applications, but doing a
|>multiplication as your argument to malloc is just asking for alignment
|>trouble. Either calloc(xL, sizeof(int *)), or (more grungily)
|>malloc(sizeof(int *[xL])) (will this work if xL is a variable?)

First, malloc doesn't know or care (and couldn't find out if it wanted
to) whether the argument being passed in is a constant expression
(like sizeof(anything)) or an expression computed at runtime via
a multiplication. My copy of K&R II says (section 7.8.5) that the
pointer returned by malloc or calloc has proper alignment for the
object in question. My manpage (SunOS 4.0.?) says that calloc USES
malloc to allocate the storage, and then initializes it to zeros.

Second, types must have a size determinable at compile time, since
sizeof yields a constant expression. So, (int *[xL]) is not a legal
type. AND EVEN IF IT WERE LEGAL, the value produced would be the
same as (xL * sizeof(int *)), so malloc wouldn't know the difference.

|>Rhodri
|>-- 
|>* Windsinger                 * "Nothing is forgotten..."
|>* rmj at islay.tcom.stc.co.uk   *                    Mike Whitaker
|>*     or (occasionally)      * "...except sometimes the words"
|>* rmj10 at phx.cam.ac.uk        *                    Phil Allcock
                                                          
==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com



More information about the Comp.lang.c mailing list