Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!mips!apple!turk From: turk@Apple.COM (Ken "Turk" Turkowski) Newsgroups: comp.lang.c++ Subject: Re: Variable sized objects Message-ID: <5891@internal.Apple.COM> Date: 20 Dec 89 06:27:47 GMT References: <1262@amethyst.math.arizona.edu> <1881@odin.SGI.COM> Organization: Advanced Technology Graphics, Apple Computer, Cupertino, CA, USA Lines: 61 In article <1881@odin.SGI.COM> shap@delrey.sgi.com (Jonathan Shapiro) writes: >First, this object cannot be built on a stack, because it's length >isn't known to the compiler. The semantic implications of a heap-only >object aren't clear. This is one of the defects of C that C++ has inherited. Consider: int MatrixInvert(double *M, rows, cols) { double LU[rows * cols]; } Here, we need a temporary (auto) array that is used only within this procedure. Unfortunately, C won't let you do this, although Fortran will (is this a step forward???). Alloca (auto allocation, from the stack) was devised to overcome this, but not all systems support it, so one is left with declaring a maximum size, hoping that it will never need to be larger: #define MAXROWS 10 #define MAXCOLS 10 int MatrixInvert(double *M, rows, cols) { #ifdef ALLOCA LU = alloca(sizeof(double) * rows * cols); #else !ALLOCA double LU[MAXROWS * MAXCOLS]; #endif ALLOCA ... } Now the analogy in C++ is: class Matrix { int _rows, _cols; double *_m; Matrix(int rows, int cols) { _rows = rows; _cols = cols; _m = new double[rows * cols]; } ~Matrix() { delete _m; } int Invert() { Matrix LU(Rows(), Cols()); ... } } In the C++ version, the new operator is called, and the corresponding delete operator is called automatically upon returning. My question is: is there a way to determine whether the matrix is auto or "new"ed? If it is possible to determine if it is auto, then alloca() could be used for allocating memory, without the tremendous overhead of new/malloc(). -- Ken Turkowski @ Apple Computer, Inc., Cupertino, CA Internet: turk@apple.com Applelink: TURKOWSKI1 UUCP: sun!apple!turk