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

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!agate!ames!saimiri.primate.wisc.edu!sdd.hp.com!caen!hellgate.utah.edu!fcom.cc.utah.edu!osiris.usi.utah.edu!tim
  3. From: tim@osiris.usi.utah.edu (Tim Burns 581-4439)
  4. Subject: Allocating memory like an array
  5. Message-ID: <1992Nov5.203939.1323@fcom.cc.utah.edu>
  6. Originator: tim@osiris.usi.utah.edu
  7. Sender: news@fcom.cc.utah.edu
  8. Reply-To: tim@osiris.usi.utah.edu (Tim Burns 581-4439)
  9. Organization: School of Engineering and Applied Sciences, UCLA
  10. Date: Thu, 5 Nov 92 20:39:39 GMT
  11. Lines: 63
  12.  
  13.  
  14. Hello,
  15.  
  16. I am trying to call LINPACK-type (ESSL) routines from a C-program and the
  17. problem I have is that the way malloc allocates memory is incompatible 
  18. with the way that arrays are allocated.  Instead of having to declare my
  19. arrays at the beginning of a program, I want to be able to allocate
  20. memory dynamically.  Right now, when I allocate my array using the 
  21. standard array allocation:  double A[L][M], where L and M are constants,
  22. everything works fine.  (Whether or not dbx can see that A is an array is 
  23. a litmus test)
  24.  
  25. (dbx) print A
  26. (
  27. (0.0, 0.0, 0.0, 0.0)
  28. (1.0, 1.0, 1.0, 1.0)
  29. (2.0, 2.0, 2.0, 2.0)
  30. )
  31. (dbx) print &A
  32. 0x2ff7f7a0
  33. (dbx) print &A[0][0]
  34. 0x2ff7f7a0
  35. (dbx) print &A[0][1]
  36. 0x2ff7f7a8
  37. (dbx) print &A[1][0]
  38. 0x2ff7f7c0
  39.  
  40. However, if I declare A using the numerical recipes suggestion of memory
  41. allocation, 
  42.  
  43.   double **m;
  44.  
  45.   m = (double **) malloc((unsigned) (nrh-nrl+1) * sizeof(double*));
  46.   if (!m) myerror("dmatrix", "allocation failure making pointers to rows.",
  47.                   FAIL_DIE);
  48.   m -= nrl;
  49.  
  50.   for ( i = nrl; i <= nrh; i++ ) {
  51.     m[i] = (double *) malloc((unsigned) (nch - ncl + 1) * sizeof(double));
  52.     if (!m[i]) myerror("dmatrix", "allocation failure allocating rows.",
  53.                        FAIL_DIE );
  54.     m[i] -= ncl;
  55.  
  56. I get a declaration which is completely different.  (What we have is a set of
  57. pointers to each row in A when m is returned to point at &A.  The memory I
  58. allocate is not guaranteed to be continues but I have experimented quite a 
  59. bit with declaring matrixes this way, and I have always gotten continues 
  60. memory.
  61.  
  62. Does anyone out there have a routine which does dynamic memory allocation
  63. which forces allocation __exactly__ the same as array allocation?  Or,
  64. can anyone give me a tip on how I might begin constructing such a utility?
  65.  
  66. Thank you for any help,
  67.  
  68. Tim Burns
  69.  
  70.  
  71. -- 
  72.   *__T__*     Even the most brilliant scientific discoveries will in time
  73.     _i_       change and perhaps grow obsolete, as new scientific mani-
  74.    */m\*      festations emerge.  But Art is eternal; for it reveals the
  75.               inner landscape which is the soul of man.  --Martha Graham
  76.