Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!zaphod.mps.ohio-state.edu!rpi!crdgw1!underdog!volpe From: volpe@underdog.crd.ge.com (Christopher R Volpe) Newsgroups: comp.lang.c Subject: Re: ** help... Message-ID: <11245@crdgw1.crd.ge.com> Date: 22 Aug 90 12:18:58 GMT References: <2409@dsacg2.dsac.dla.mil> <1884@jura.tcom.stc.co.uk> Sender: news@crdgw1.crd.ge.com Reply-To: volpe@underdog.crd.ge.com (Christopher R Volpe) Lines: 36 In article <1884@jura.tcom.stc.co.uk>, rmj@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@islay.tcom.stc.co.uk * Mike Whitaker |>* or (occasionally) * "...except sometimes the words" |>* rmj10@phx.cam.ac.uk * Phil Allcock ================== Chris Volpe G.E. Corporate R&D volpecr@crd.ge.com