home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / amiga / programm / 17673 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  2.2 KB

  1. Path: sparky!uunet!wupost!waikato.ac.nz!comp.vuw.ac.nz!actrix!templar!jbickers
  2. Newsgroups: comp.sys.amiga.programmer
  3. Subject: Re: HELP - malloc, realloc
  4. Message-ID: <jbickers.0lum@templar.actrix.gen.nz>
  5. From: jbickers@templar.actrix.gen.nz (John Bickers)
  6. Date: 22 Dec 92 13:23:38 PST
  7. References: <Douglas_Stockman.1lyc@grace.UUCP>
  8. Organization: TAP
  9. Lines: 74
  10.  
  11.     You may be able to circumvent all this by changing the way you
  12.     handle your array. For example:
  13.  
  14.     typedef struct MATRIX3 {
  15.         char    *p;
  16.         int     ni,nj,nk;
  17.     } MATRIX3;
  18.  
  19.     MATRIX3 *matrix3(ni,nj,nk)
  20.     int     ni,nj,nk;
  21.     {
  22.     MATRIX3 *m3p;
  23.         m3p = malloc(sizeof(MATRIX3));
  24.         if (m3p) {
  25.             m3p->p = malloc(ni * nj * nk * sizeof(char));
  26.             if (m3p->p) {
  27.                 m3p->ni = ni;
  28.                 m3p->nj = nj;
  29.                 m3p->nk = nk;
  30.             }
  31.             else {
  32.                 free(m3p);
  33.                 m3p = NULL;
  34.             }
  35.         }
  36.         return(m3p);
  37.     }
  38.  
  39.     char    getel(m3p,i,j,k)
  40.     MATRIX3 *m3p;
  41.     int     i,j,k;
  42.     {
  43.         return(*(m3p->p + (i * m3p->ni) + (j * m3p->nj) + k));
  44.     }
  45.  
  46.     void    putel(m3p,i,j,k,ch)
  47.     MATRIX3 *m3p;
  48.     int     i,j,k;
  49.     char    ch;
  50.     {
  51.         *(m3p->p + (i * m3p->ni) + (j * m3p->nj) + k) = ch;
  52.     }
  53.  
  54.     MATRIX3 *rematrix3(m3p,newi,newj,newk)
  55.     MATRIX3 *m3p;
  56.     int     newi,newj,newk;
  57.     {
  58.     MATRIX3 *np;
  59.     int     i,j,k;
  60.  
  61.         np = matrix3(newi,newj,newk);
  62.         if (!np) uh oh
  63.  
  64.         for (i = 0; i < MIN(np->ni,m3p->ni); i++) {
  65.             for (j = 0; j < MIN(np->nj,m3p->nj); j++) {
  66.                 for (k = 0; k < MIN(np->nk,m3p->nk); k++) {
  67.                     putel(np,i,j,k,getel(m3p,i,j,k));
  68.                 }
  69.             }
  70.         }
  71.  
  72.         free(m3p->p);
  73.         free(m3p);
  74.         return(np);
  75.     }
  76.  
  77.     If what you're trying to represent is a two-dimensional array of
  78.     zero-terminated strings, you can get some speed back if you get
  79.     rid of the "k" parameter in getel(), putel() and rematrix3(), use
  80.     character pointers rather than just characters, and use strcpy()
  81.     instead of just '='.
  82. --
  83. *** John Bickers, TAP.                   jbickers@templar.actrix.gen.nz ***
  84. ***    "Radioactivity - It's in the air, for you and me" - Kraftwerk    ***
  85.