Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!hsdndev!spdcc!ima!dirtydog!karl From: karl@ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: 2D arrays and pointers in ANSI C. (source) Keywords: malloc, arrays, arrayn Message-ID: <1990Dec04.201944.2094@dirtydog.ima.isc.com> Date: 4 Dec 90 20:19:44 GMT References: <11616.2757eaf1@ecs.umass.edu> <1990Dec2.125713.19371@batcomputer.tn.cornell.edu> Sender: news@dirtydog.ima.isc.com (NEWS ADMIN) Reply-To: karl@ima.isc.com (Karl Heuer) Organization: Interactive Systems Lines: 26 In article <1990Dec2.125713.19371@batcomputer.tn.cornell.edu> christos@batcomputer.tn.cornell.edu (Christos S. Zoulas) writes: >I think the following piece of code will solve your problem: >[code to implement a function that purports to allow allocations like:] > ptr = (thing **)arrayn(2, M, N, sizeof(thing)); > ... ptr[i][j] ... Although this probably works fine on vaxlike architectures, it will fail on machines where `void *' and `thing *' have different internal representations (such as DG). This cannot be fixed without help from a compiler builtin, and hence any program that uses arrayn() is unportable. The only way this could become portable would be if the Standard were to mandate its existence% (and hence require nonvaxlike implementations to supply a simple crutch as a compiler builtin). The Committee probably won't do so unless there's existing practice, and they probably won't accept existing practice because it's unportable code. (Nice job, though. I never bothered with more than 2D in my implementation, before I threw it out for the reason described above.) Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint ________ % The "standardizable" version would have to be a macro which expects one parameter to be a type name (e.g. array2(thing, M, N)), since "sizeof" might throw out too much information. This is a further complication for the generic version, since C does not support variadic macros. Brought to you by Super Global Mega Corp .com