Article View: hepnet.lang.c++
Article #6Re: dynamicaly allocating a two dim. array
From: 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 ]
Technical Details
Message-ID:
<4766cq$7fa@newshost.lanl.gov>Path:
arc1-ca.pugleaf.net!qcd.lanl.gov!tanmoyReferences:
<DG7ExA.3CG@cunews.carleton.ca> <dqua.813429186@dqua> <474ljk$551@nic.dataphone.se> <4762q8$1gaq@news.gate.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!qcd.lanl.gov!tanmoy From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya) Newsgroups: hepnet.lang.c++,comp.lang.c,comp.lang.c++,cern.c++ Subject: Re: dynamicaly allocating a two dim. array Date: 31 Oct 1995 22:02:02 GMT Organization: Los Alamos National Laboratory Distribution: world Message-ID: <4766cq$7fa@newshost.lanl.gov> References: <DG7ExA.3CG@cunews.carleton.ca> <dqua.813429186@dqua> <474ljk$551@nic.dataphone.se> <4762q8$1gaq@news.gate.net> NNTP-Posting-Host: qcd.lanl.gov