Article View: hepnet.lang.c++
Article #9Re: dynamicaly allocating a two dim. array
From: John R Buchan (jatmon)
Date: Wed, 01 Nov 1995 02:39
Date: Wed, 01 Nov 1995 02:39
38 lines
1616 bytes
1616 bytes
In article <474ljk$551@nic.dataphone.se>, skorpio@dataphone.sep says...
>
>In message <46d9ak$c5h@ixnews7.ix.netcom.com> - techman2@ix.netcom.com (Peter
>Dennis ) writes:
>
>:> float array[x][y];
>
>First C++ version
>
>float **array;
>
>array = new float*[x];
>for(i = 0;i < x;i++)
> { array[i] = new float[y]; }
>
>and then C
>
>float **array;
>
>array = (float**)malloc(sizeof(float)*x);
^^^^^^^^^^^^
Make this sizeof(float*).
There is no need to cast the return of malloc and, since it can conceivably
hide errors, you might want to consider avoiding unnecessary casts.
Of course, you should always test any allocatio for success, before trying
to use it. I assume you left the test out for brevity's sake.
>for(i = 0;i < x;i++)
> { array[i] = (float*)malloc(sizeof(float)*y); }
The c.l.c faq has several examples of this (Q6.16). It is available for
anonymous ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.
--
John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
Technical Details
Message-ID:
<476mka$blh@news.iag.net>Path:
arc1-ca.pugleaf.net!news.iag.net!usenetReferences:
<DG7ExA.3CG@cunews.carleton.ca> <dqua.813429186@dqua> <474ljk$551@nic.dataphone.se>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.iag.net!usenet From: jatmon@iag.net (John R Buchan (jatmon)) Newsgroups: hepnet.lang.c++,comp.lang.c,comp.lang.c++,cern.c++ Subject: Re: dynamicaly allocating a two dim. array Date: 1 Nov 1995 02:39:06 GMT Organization: Internet Access Group, Orlando, Florida Message-ID: <476mka$blh@news.iag.net> References: <DG7ExA.3CG@cunews.carleton.ca> <dqua.813429186@dqua> <474ljk$551@nic.dataphone.se> NNTP-Posting-Host: pm3-orl18.iag.net X-Newsreader: WinVN 0.99.6