Article View: hepnet.lang.c++
Article #5Re: dynamicaly allocating a two dim. array
From: William Hutto
Date: Tue, 31 Oct 1995 21:00
Date: Tue, 31 Oct 1995 21:00
57 lines
1828 bytes
1828 bytes
In article <474ljk$551@nic.dataphone.se>,
skorpio@dataphone.se (Jarmo Paavilainen) wrote:
>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);
>for(i = 0;i < x;i++)
> { array[i] = (float*)malloc(sizeof(float)*y); }
>
>Code not tested but it should work ( the C++ version anyway ).
>
>
>Take care,
>Skorpio
>
>ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͻ
>ºJarmo Paavilainen ( alias : Skorpio ) ³e-mail : skorpio@dataphone.se º
>ºSweden ³home : ? º
>ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
Is the following not true?
Assuming:
[type] array[x][y]
array[i1][i2] == *(array + (x*i2 + i1) * (sizeof)[type])
AND
array[i1][i2] == *(array + (y*i1 + i2) * (sizeof)[type])
and if the compiler calculates 2 dim arrays using one of these methods a
dynamic allocation in C would consist of:
[type] *array;
array = ([type] *)malloc(x*y);
and references to an element in the array be in the form:
array[i1][i2];
Bill
Technical Details
Message-ID:
<4762q8$1gaq@news.gate.net>Path:
arc1-ca.pugleaf.net!gate.net!pslfl2-51References:
<DG7ExA.3CG@cunews.carleton.ca> <dqua.813429186@dqua> <474ljk$551@nic.dataphone.se>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!gate.net!pslfl2-51 From: bhutto@gate.net (William Hutto) Newsgroups: hepnet.lang.c++,comp.lang.c,comp.lang.c++,cern.c++ Subject: Re: dynamicaly allocating a two dim. array Date: 31 Oct 1995 21:00:56 GMT Organization: CyberGate Message-ID: <4762q8$1gaq@news.gate.net> References: <DG7ExA.3CG@cunews.carleton.ca> <dqua.813429186@dqua> <474ljk$551@nic.dataphone.se> NNTP-Posting-Host: pslfl2-51.gate.net X-Newsreader: News Xpress Version 1.0 Beta #4