home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!waikato.ac.nz!comp.vuw.ac.nz!actrix!templar!jbickers
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: HELP - malloc, realloc
- Message-ID: <jbickers.0lum@templar.actrix.gen.nz>
- From: jbickers@templar.actrix.gen.nz (John Bickers)
- Date: 22 Dec 92 13:23:38 PST
- References: <Douglas_Stockman.1lyc@grace.UUCP>
- Organization: TAP
- Lines: 74
-
- You may be able to circumvent all this by changing the way you
- handle your array. For example:
-
- typedef struct MATRIX3 {
- char *p;
- int ni,nj,nk;
- } MATRIX3;
-
- MATRIX3 *matrix3(ni,nj,nk)
- int ni,nj,nk;
- {
- MATRIX3 *m3p;
- m3p = malloc(sizeof(MATRIX3));
- if (m3p) {
- m3p->p = malloc(ni * nj * nk * sizeof(char));
- if (m3p->p) {
- m3p->ni = ni;
- m3p->nj = nj;
- m3p->nk = nk;
- }
- else {
- free(m3p);
- m3p = NULL;
- }
- }
- return(m3p);
- }
-
- char getel(m3p,i,j,k)
- MATRIX3 *m3p;
- int i,j,k;
- {
- return(*(m3p->p + (i * m3p->ni) + (j * m3p->nj) + k));
- }
-
- void putel(m3p,i,j,k,ch)
- MATRIX3 *m3p;
- int i,j,k;
- char ch;
- {
- *(m3p->p + (i * m3p->ni) + (j * m3p->nj) + k) = ch;
- }
-
- MATRIX3 *rematrix3(m3p,newi,newj,newk)
- MATRIX3 *m3p;
- int newi,newj,newk;
- {
- MATRIX3 *np;
- int i,j,k;
-
- np = matrix3(newi,newj,newk);
- if (!np) uh oh
-
- for (i = 0; i < MIN(np->ni,m3p->ni); i++) {
- for (j = 0; j < MIN(np->nj,m3p->nj); j++) {
- for (k = 0; k < MIN(np->nk,m3p->nk); k++) {
- putel(np,i,j,k,getel(m3p,i,j,k));
- }
- }
- }
-
- free(m3p->p);
- free(m3p);
- return(np);
- }
-
- If what you're trying to represent is a two-dimensional array of
- zero-terminated strings, you can get some speed back if you get
- rid of the "k" parameter in getel(), putel() and rematrix3(), use
- character pointers rather than just characters, and use strcpy()
- instead of just '='.
- --
- *** John Bickers, TAP. jbickers@templar.actrix.gen.nz ***
- *** "Radioactivity - It's in the air, for you and me" - Kraftwerk ***
-