[rescue] Cooling (Long Message, sorry)

Joshua D Boyd jdboyd at cs.millersville.edu
Thu Apr 18 09:36:27 CDT 2002


On Thu, Apr 18, 2002 at 08:47:50AM -0500, Scott Newell wrote:
> >How are you supposed to make variable sized float[][]s? 
> 
> new or malloc(), just like with variable sized single dimension arrays!
 
So, is float **f = (float**)new float[x][y]; valid or not?  Big Endian yelled
on list over that, but it does work, and it contains the word new in it.
 
> >Besides, how is new float[x][y] going to screw things up?  They had to have
> >some reason for making it a legal operation.
> 
> You can allocate an array on the stack if the dimensions are constants
> (known at compile time).  Otherwise, you need to allocate off the heap
> using malloc() or new.

isn't f=new float[x][y] taking it from the heap?  How can new do anything but
take it from the heap?  new float[x][y] is in the line you replied to.

> http://www.eskimo.com/~scs/cclass/int/sx9b.html
> 
> http://www.eskimo.com/~scs/C-faq/questions.html

Or, perhaps you liked my original method of doing

float **f = (float**)new int[x];
for(int i=0; i<x; i++) f[i] = new float[y];

Although, that makes the assumption that the size of pointers is the same
as the size of ints.  That is the closest C++ method I see to the method used
in the first URL.

Not to be rude, but saying the proper way to do this in C is not helpful, since
I (and I'm assuming most other people who are reading this) already know the
proper way to do it in C.  The problem is what is the proper way to do it in
C++, and using malloc is virtually never the proper answer for C++ (I can only
think of one reason to use malloc in c++, and that only applies to windows, to
my understanding).

There are a number of reasons to use C++.  One reason is because professors
and/or jobs require it.  Another reason is because classes really are 
usefull, as is streams based IO.  And if you are going to use a language, you
should use it properly.  Although, I suspect there is no such thing as 
properly for Visual Basic.

-- 
Joshua D. Boyd



More information about the rescue mailing list