🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.c
Article #202

Re: dynamiczne tablice wielowymiarowe

#202
From: Witold Przygoda
Date: Sat, 01 Nov 1997 18:12
109 lines
3635 bytes
dobrov@friko.onet.pl (Arkadiusz Dabrowski) wrote:

>>int **owaZmienna;
>>
>>owaZmienna = new (int*)[5];
>>for (int i=0; i<5; i++)  owaZmienna[i] = new int[n];
>
>Zupelnie nie tak. Alokujesz 5 wskaznikow na inta, a nie tablice
>wielowymiarowa.
>
>
>Ma byc tak:
>
>int (*a)[5];
>a=new int[n][5];   // jak zamiast 5 napiszesz cos innego, to kompilator
>                   // wglosi niezgodnosc typow (przynajmniej dobry kompilator tak zrobi)


Moje rozwiazanie nie jest w sensie scislym tablica 2-wymiarowa, bo
najpierw mam niejako wskazniki do 5 rzedow, ktory potem kazdy z nich
dodaje n kolumn. W efekcie ma cos. czego potrzebowalem, a
potrzebowalem owaZmienna[n][5];   gdzie n - daje jako dana z zewnatrz.
Dziala.

Poza tym znalazlem na i-necie jeszcze pare innych rozwiazan, np.:

int **AllocateCont2dArray(int rows, int cols)
{
   int **m, *data;
   int i;

   m = new (int*)[rows]          // Array of pointers to the rows.
   data = new int[rows*cols]     // Allocate a contiguous section of
memory
                                 // for the whole matrix.
   for(i = 0; i < rows; i++)
     m[i] = &data[i*cols];       // `Connect' each row pointer to the
correct
                                 // location in the 1D array
   return(m);
}



albo (tu przytocze cala liste...........)


TITLE: Allocating multidimensional arrays


I have declared:
        double multiarray[][1000] ;
and now want to allocate storage.  What is the proper syntax for a
'new'
statement allocating space for a [16][1000] array?  The C++ faq
doesn't
seem to have anything on multidimensional arrays.

.............................................................................

Try this:

double **array = new double* [sizeof(double*) * rows];
array[0] = new double [sizeof(double) * rows * columns];
for (int i = 1; i < rows; i++)
        array[i] = array[0] + i * columns;

Where array is list of pointers to the beginning of each row.
Now you can just write array[i][j] to reference the elements.

.............................................................................


typedef double dmill[1000];
dmill *multiarray=new dmill[16];

.............................................................................


The final solution to my problem was:
double (*mutiDimArrayPtr)[1000];
.
.
.
mutiDimArrayPtr = new double[16][1000] ;

p.s.  I think some info on multidimensional arrays belongs in the FAQ.
This stuff is not simple!

.............................................................................


Never try something complicated when you can simplify it.
NEVER instantiate multidimeniosnal arrays.

USE A TYPEDEF.

typdef int i3[3];
typdef i3 i35[5];

i35*p=new i35[7];

No confusion here. There are 7 i35 objects, each is 5 i3 objects
each of which is an array of 3 integers.




Pozdrawiam i dzieki za odzew, wp
Technical Details
Message-ID: <345b6eaf.217411093@news.uci.agh.edu.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!uw.edu.pl!uucp-gw.cyf.gov.pl!polsl.gliwice.pl!agh.edu.pl!not-for-mail
References: <34631720.129460531@news.uci.agh.edu.pl> <34643383.136727859@news.uci.agh.edu.pl> <345a58d8.616700@news.icm.edu.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!uw.edu.pl!uucp-gw.cyf.gov.pl!polsl.gliwice.pl!agh.edu.pl!not-for-mail
From: wp@cyberdude.com (Witold Przygoda)
Newsgroups: pl.comp.lang.c
Subject: Re: dynamiczne tablice wielowymiarowe
Date: Sat, 01 Nov 1997 18:12:11 GMT
Organization: University of Mining & Metallurgy
Lines: 109
Message-ID: <345b6eaf.217411093@news.uci.agh.edu.pl>
References: <34631720.129460531@news.uci.agh.edu.pl> <34643383.136727859@news.uci.agh.edu.pl> <345a58d8.616700@news.icm.edu.pl>
NNTP-Posting-Host: wfr001.gsi.de
Mime-Version: 1.0
X-Newsreader: Forte Agent 1.5/32.451
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit