home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / c / 2979 < prev    next >
Encoding:
Text File  |  1992-11-08  |  2.1 KB  |  51 lines

  1. Newsgroups: comp.std.c
  2. 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
  3. From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
  4. Subject: Re: Allocating memory like an array
  5. Message-ID: <BxAAx5.ALI@jrd.dec.com>
  6. Sender: usenet@jrd.dec.com (USENET News System)
  7. Nntp-Posting-Host: jit345
  8. Reply-To: diamond@jit.dec.com (Norman Diamond)
  9. Organization: Digital Equipment Corporation Japan , Tokyo
  10. References: <1992Nov5.203939.1323@fcom.cc.utah.edu>
  11. Date: Fri, 6 Nov 1992 07:46:16 GMT
  12. Lines: 37
  13.  
  14. In article <1992Nov5.203939.1323@fcom.cc.utah.edu> tim@osiris.usi.utah.edu (Tim Burns 581-4439) writes:
  15. >(dbx) print A
  16.  
  17. That's not a standards question.  However, the remaining portion is
  18. borderline (IMO) and maybe useful to enough people, here goes.
  19.  
  20. >if I declare A using the numerical recipes suggestion of memory allocation, 
  21. >  double **m;
  22. >  m = (double **) malloc((unsigned) (nrh-nrl+1) * sizeof(double*));
  23. >  if (!m) myerror("dmatrix", "allocation failure making pointers to rows.",
  24. >                  FAIL_DIE);
  25. >  m -= nrl;
  26. >  for ( i = nrl; i <= nrh; i++ ) {
  27. >    m[i] = (double *) malloc((unsigned) (nch - ncl + 1) * sizeof(double));
  28. >    if (!m[i]) myerror("dmatrix", "allocation failure allocating rows.",
  29. >                       FAIL_DIE );
  30. >    m[i] -= ncl;
  31. >The memory I allocate is not guaranteed to be continues but I have
  32. >experimented quite a bit [...] always gotten continues memory.
  33.  
  34. You haven't experimented enough, I promise you.
  35.  
  36.   double **m;
  37.   double *m0;
  38.   size_t nr = nrh - nrl + 1;  /* or int or whatever, same as i, nrl, etc. */
  39.   size_t nc = nch - ncl + 1;
  40.   m0 = malloc(nr * nc * sizeof(double));
  41.   if (!m0) myerror("dmatrix", "allocation failure making matrix.", FAIL_DIE);
  42.   m = malloc(nr * sizeof(double*));
  43.   if (!m) myerror("dmatrix", "allocation failure making pointers.", FAIL_DIE);
  44.   for ( i = nrl; i <= nrh; i++ ) {
  45.     m[i] = m0 + i * nc;
  46.   }
  47. --
  48. Norman Diamond       diamond@jit081.enet.dec.com
  49. If this were the company's opinion, I wouldn't be allowed to post it.
  50. "It's been a lovely recession."
  51.