Article View: comp.std.c
Article #2070Allocating memory like an array
From: Tim Burns 581-4439
Date: Thu, 05 Nov 1992 20:39
Date: Thu, 05 Nov 1992 20:39
63 lines
2761 bytes
2761 bytes
Hello,
I am trying to call LINPACK-type (ESSL) routines from a C-program and the
problem I have is that the way malloc allocates memory is incompatible
with the way that arrays are allocated. Instead of having to declare my
arrays at the beginning of a program, I want to be able to allocate
memory dynamically. Right now, when I allocate my array using the
standard array allocation: double A[L][M], where L and M are constants,
everything works fine. (Whether or not dbx can see that A is an array is
a litmus test)
(dbx) print A
(
(0.0, 0.0, 0.0, 0.0)
(1.0, 1.0, 1.0, 1.0)
(2.0, 2.0, 2.0, 2.0)
)
(dbx) print &A
0x2ff7f7a0
(dbx) print &A[0][0]
0x2ff7f7a0
(dbx) print &A[0][1]
0x2ff7f7a8
(dbx) print &A[1][0]
0x2ff7f7c0
However, 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;
I get a declaration which is completely different. (What we have is a set of
pointers to each row in A when m is returned to point at &A. The memory I
allocate is not guaranteed to be continues but I have experimented quite a
bit with declaring matrixes this way, and I have always gotten continues
memory.
Does anyone out there have a routine which does dynamic memory allocation
which forces allocation __exactly__ the same as array allocation? Or,
can anyone give me a tip on how I might begin constructing such a utility?
Thank you for any help,
Tim Burns
--
*__T__* Even the most brilliant scientific discoveries will in time
_i_ change and perhaps grow obsolete, as new scientific mani-
*/m\* festations emerge. But Art is eternal; for it reveals the
inner landscape which is the soul of man. --Martha Graham
Technical Details
Message-ID:
<1992Nov5.203939.1323@fcom.cc.utah.edu>Path:
arc1-ca.pugleaf.net!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!timShow Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!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 From: tim@osiris.usi.utah.edu (Tim Burns 581-4439) Subject: Allocating memory like an array Message-ID: <1992Nov5.203939.1323@fcom.cc.utah.edu> Originator: tim@osiris.usi.utah.edu Sender: news@fcom.cc.utah.edu Reply-To: tim@osiris.usi.utah.edu (Tim Burns 581-4439) Organization: School of Engineering and Applied Sciences, UCLA Date: Thu, 5 Nov 92 20:39:39 GMT Lines: 63