[rescue] Cooling (Long Message, sorry)

Joshua D Boyd jdboyd at cs.millersville.edu
Tue Apr 16 22:09:57 CDT 2002


On Tue, Apr 16, 2002 at 08:57:32PM -0400, Big Endian wrote:
> >For instance, wanting a float[x][y] today.  I ended up doing:
> >
> >float **f;
> >int *x=new int[x];
> >f =(float**)x;
> >float *g;
> >
> >for (int i=0; i<y; i++) {
> >	g = new float[y];
> >	f[i]=g;
> >}
> 
> ok... WHY?  Assuming x and y are fixed values you could just do this:
> 
> float f[x][y];
> 
> and use nested for loops to initialize it?

#include <iostream>

float * test() {
  int x = 3;
  int y = 4;

  float f[x][y];

  return (float *)f;

}

void main() {

  cout << (int)test() << endl;
  cout << (int)test() << endl;

}

The output from running that is:
-1073743424
-1073743424

Meaning, that f isn't a new memory location each time, which is a major
problem since instead of test() here, I was writing a function that constructs
a matrix that then needs to persist past the creation of the next one, and also
gets passed to third party libraries.

-- 
Joshua D. Boyd



More information about the rescue mailing list