Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!rutgers!sri-spam!ames!lll-crg!lll-lcc!pyramid!voder!apple!turk From: turk@apple.UUCP (Ken "Turk" Turkowski) Newsgroups: comp.lang.c Subject: Re: help needed about dynamic space allocation Message-ID: <332@apple.UUCP> Date: Wed, 26-Nov-86 16:33:00 EST Article-I.D.: apple.332 Posted: Wed Nov 26 16:33:00 1986 Date-Received: Wed, 26-Nov-86 22:07:00 EST References: <13802@amdcad.UUCP> <359@prairie.UUCP> <1046@lifia.UUCP> <715@dg_rtp.UUCP> Reply-To: turk@apple.UUCP (Ken "Turk" Turkowski) Organization: Apple Computer Inc., Cupertino, USA Lines: 38 >> fano@lifia.UUCP (Rampa*) >> I need advice on dynamic space allocation for multidimensionnal arrays: >> >> float **sig; /* matrix declaration */ >> sig = (int *)malloc(n * sizeof(int)); /* allocates space for sig */ There have been numerous responses to get the declarations consistent, so I won't belabor them here. However, I would like to point out the use of a function that can speed up the program considerably when you are only allocating temporary variables. The function is alloca(), and allocates memory from the stack, rather than from the heap. The result is that allocation is simpler, and vanishes automatically when you exit the routine. The purpose of alloca() is to compensate for the deficiency in C that does not allow you to do something like this: func(a, n) int *a, n; { int b[n+1]; /* <- C doesn't allow this */ . . . } by doing this: func(a, n) int *a, n; { int *b; b = (int *)(alloca((n+1) * sizeof(int))); . . . -- Ken Turkowski @ Apple Computer, Inc., Cupertino, CA UUCP: {sun,nsc}!apple!turk CSNET: turk@Apple.CSNET ARPA: turk%Apple@csnet-relay.ARPA