home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c
- Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!ames!haven.umd.edu!decuac!pa.dec.com!jrdzzz.jrd.dec.com!jit345.bad.jit.dec.com!diamond
- From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
- Subject: Re: Allocating memory like an array
- Message-ID: <BxAAx5.ALI@jrd.dec.com>
- Sender: usenet@jrd.dec.com (USENET News System)
- Nntp-Posting-Host: jit345
- Reply-To: diamond@jit.dec.com (Norman Diamond)
- Organization: Digital Equipment Corporation Japan , Tokyo
- References: <1992Nov5.203939.1323@fcom.cc.utah.edu>
- Date: Fri, 6 Nov 1992 07:46:16 GMT
- Lines: 37
-
- In article <1992Nov5.203939.1323@fcom.cc.utah.edu> tim@osiris.usi.utah.edu (Tim Burns 581-4439) writes:
- >(dbx) print A
-
- That's not a standards question. However, the remaining portion is
- borderline (IMO) and maybe useful to enough people, here goes.
-
- >if I declare A using the numerical recipes suggestion of memory allocation,
- > double **m;
- > m = (double **) malloc((unsigned) (nrh-nrl+1) * sizeof(double*));
- > if (!m) myerror("dmatrix", "allocation failure making pointers to rows.",
- > FAIL_DIE);
- > m -= nrl;
- > for ( i = nrl; i <= nrh; i++ ) {
- > m[i] = (double *) malloc((unsigned) (nch - ncl + 1) * sizeof(double));
- > if (!m[i]) myerror("dmatrix", "allocation failure allocating rows.",
- > FAIL_DIE );
- > m[i] -= ncl;
- >The memory I allocate is not guaranteed to be continues but I have
- >experimented quite a bit [...] always gotten continues memory.
-
- You haven't experimented enough, I promise you.
-
- double **m;
- double *m0;
- size_t nr = nrh - nrl + 1; /* or int or whatever, same as i, nrl, etc. */
- size_t nc = nch - ncl + 1;
- m0 = malloc(nr * nc * sizeof(double));
- if (!m0) myerror("dmatrix", "allocation failure making matrix.", FAIL_DIE);
- m = malloc(nr * sizeof(double*));
- if (!m) myerror("dmatrix", "allocation failure making pointers.", FAIL_DIE);
- for ( i = nrl; i <= nrh; i++ ) {
- m[i] = m0 + i * nc;
- }
- --
- Norman Diamond diamond@jit081.enet.dec.com
- If this were the company's opinion, I wouldn't be allowed to post it.
- "It's been a lovely recession."
-