Thread View: hepnet.lang.c++
8 messages
8 total messages
Started by Bart Trojanowski
Mon, 09 Oct 1995 23:07
dynamicaly allocating a two dim. array
Author: Bart Trojanowski
Date: Mon, 09 Oct 1995 23:07
Date: Mon, 09 Oct 1995 23:07
33 lines
1289 bytes
1289 bytes
I desperately need help. My assignment is due very soon. What I need to do is allocate memory for the following.... float array[x][y]; but dinamicaly, ie the user specifies how large it must be. float = **array; x=user_input(); y=user_input(); *array = new (float*) [x]; for (a=0;a<x;x++) array[a] = new float [y]; ... for (a=0;a<x;x++) delete array[a]; delete *array; ... how come this works in dos and under windows it gives me a funkey protection fault. ------------------------------------------------------------------------------ Bart Trojanowski Carleton University School O'Computer Science btrojano@chat.carleton.ca ab527@freenet.carleton.ca ------------------------------------------------------------------------------
Re: dynamicaly allocating a two dim. array
Author: Sam Denton
Date: Tue, 10 Oct 1995 00:07
Date: Tue, 10 Oct 1995 00:07
18 lines
1063 bytes
1063 bytes
btrojano@chat.carleton.ca (Bart Trojanowski) revealed:
|for (a=0;a<x;x++)
--------------^ I assume you mean "a"
| delete array[a];
|delete *array;
|how come this works in dos and under windows it gives me a funkey
|protection fault.
Because you have an incorrect program. Note that your second delete
is deleting whatever the first element of array is pointing to, not
array itself. So you are deleting one thing twice and the other not
at all. (Also, you aren't using the correct form of delete here.)
Try this:
for (a=0;a<x;a++)
delete [] array[a];
delete [] array;
Re: dynamicaly allocating a two dim. array
Author: Derick J.R. Qua-Gonzalez
Date: Wed, 11 Oct 1995 16:33
Date: Wed, 11 Oct 1995 16:33
48 lines
1772 bytes
1772 bytes
btrojano@chat.carleton.ca (Bart Trojanowski) writes:
>I desperately need help. My assignment is due very soon.
This shouldn't be in c.l.c. But since you xposted to C++ as wel...
>What I need to do is allocate memory for the following....
> float array[x][y];
>but dinamicaly, ie the user specifies how large it must be.
>float = **array;
|
|
WHAT?
I think you mean ``float **array;''
>*array = new (float*) [x];
|
+---- OH OH!
array = new (float*)[x];
>for (a=0;a<x;x++)
> array[a] = new float [y];
>for (a=0;a<x;x++)
> delete array[a];
>delete *array;
Since each of array[a] are themselves arrays, this line should read:
delete[] array[a];
and further:
delete[] array;
// DQG.
--
+------------------------------------------------------------------------+
| Derick R. Qua-Gonzalez | ________ |
| Department of Physics, California State University | \ / |
| dqua@EarthLink.Net | \ / |
| ``It is better to be hated for what one is, | \ / |
| than loved for what one is not.'' (A. Gide) | 32 \/ USA|
+-------------------------------------------------------------+----------+
Re: dynamicaly allocating a two dim. array
Author: Peter Dennis
Date: Sun, 22 Oct 1995 11:18
Date: Sun, 22 Oct 1995 11:18
76 lines
2226 bytes
2226 bytes
In article <dqua.813429186@dqua>,
dqua@earthlink.net (Derick J.R. Qua-Gonzalez) wrote:
>btrojano@chat.carleton.ca (Bart Trojanowski) writes:
>
>>I desperately need help. My assignment is due very soon.
>
>This shouldn't be in c.l.c. But since you xposted to C++ as wel...
>
>>What I need to do is allocate memory for the following....
>> float array[x][y];
>>but dinamicaly, ie the user specifies how large it must be.
>
>>float = **array;
> |
> |
> WHAT?
>
Hope my idea will help you! (No guarantees on correctness)
but...
If I understand your problem, you want to make a two dimensional array
of
floats by using new after you get the two indeces from the user?
user enters x and y(two ints)
if you declare array a pointer to a float...
float *array;
then use new as follows...
In c++...
array = new float[x*y];
In c...
array = (float*) malloc(sizeof(float)*x*y);
this should create an array of floats in memory which you can be used
like this(I think)
array[a*y+b] where as array[a][b] is what you want.
float *array;
x = 3;
y = 4;
array = new float[x*y];
/* assign array[2][3] to be 3.14159 */
array[2*y + 3] = 3.14159;
in c++...
cout << "array[" << 2 << "][" << 3 << "] = " << array[2*y+3] << endl;
or
cout << "array[2][3] = " << array[2*y+3] << endl;
in c...
printf("array[%d][%d] = %f\n", 2, 3, array[2*y + 3] );
or
printf("array[2][3] = %f\n", array[2*y +3] );
output should be...
with printf in c I got
array[2][3] = 3.141590
with cout in c++ I got
array[2][3] = 3.14159
to work with array[a][b] in array[x][y] just use array[a*y+b]
Hope I didn't screw up somewhere.
Use this information at your own risk.
TechMan2@ix.netcom.com
Re: dynamicaly allocating a two dim. array
Author: Jarmo Paavilainen
Date: Tue, 31 Oct 1995 08:09
Date: Tue, 31 Oct 1995 08:09
31 lines
1273 bytes
1273 bytes
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 : ? º
ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
Re: dynamicaly allocating a two dim. array
Author: 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
Re: dynamicaly allocating a two dim. array
Author: Tanmoy Bhattacharya
Date: Tue, 31 Oct 1995 22:02
Date: Tue, 31 Oct 1995 22:02
65 lines
2789 bytes
2789 bytes
In article <4762q8$1gaq@news.gate.net>, bhutto@gate.net (William Hutto) writes: <snip> |> Is the following not true? |> |> Assuming: |> |> [type] array[x][y] One cannot `assume' this if x and y are not constants. There is _no_ variable dimension array type in standard C. |> |> array[i1][i2] == *(array + (x*i2 + i1) * (sizeof)[type]) |> AND |> array[i1][i2] == *(array + (y*i1 + i2) * (sizeof)[type]) Not quite. The problem is that the meaning of pointer addition depends on the type of the pointer. Assuming x and y are constants and the above declaration of array, the value of `array' is &array[0] which has the type `[type] (*) [y]' i.e. a pointer to an array [y] of [type]. Adding 1 to it skips over not one `byte', but over an entire array [y] of [type], i.e. over sizeof ([type][y]) bytes, i.e. over y*sizeof([type]) bytes. You also probably did not mean to write (sizeof)[type]. And if you think a bit, you will see that both your statements cannot be true simultaneously. (Easy way to see this, let x != y, where is array[1][1] stored? Your first line says at x+1 offset whereas the second one says y+1!) I am going to assume that you meant * ([type]*) ( (char*) array + (y*i1+i2) * sizeof ([type]) ) Note that this is correct _only if_ the declaration of array is [type] array[x][y] |> |> 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]; But your declaration is different!!! With [type] *array the meaning of array[i1][i2] is completely different, and is most often a syntax error!!! Cheers Tanmoy -- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242) Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544 Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>, <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/ internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html> fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
Re: dynamicaly allocating a two dim. array
Author: 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/....
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads