home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / amiga / programm / 17556 < prev    next >
Encoding:
Text File  |  1992-12-21  |  1.8 KB  |  64 lines

  1. Path: sparky!uunet!amusing!grace!Douglas_Stockman
  2. From: Douglas_Stockman@grace.UUCP (Douglas Stockman)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: HELP - malloc, realloc
  5. Distribution: world
  6. Message-ID: <Douglas_Stockman.1lyc@grace.UUCP>
  7. Date: 18 Dec 92 09:39:21 EST
  8. Organization: Graceful Boot BBS (send replies to grace!User_Name@moscom.com)
  9. Lines: 53
  10.  
  11. [[AAIn response to jbickers@templar.actrix.gen.nz (John Bickers)'s message:
  12. >    You should be able to get away with:
  13. >
  14. >        ptr = (cast)realloc(ptr,newsize);
  15. >
  16. >    What goes wrong with that?
  17.  
  18.  
  19. I may be missing something, but the answer seems more complicated to me.
  20. To perform the initial malloc I use something like the following:
  21.  
  22.     char ***temp;   /** equivalent to temp[row][col][cell]  **/
  23.  
  24.     main
  25.     {
  26.         temp = matrix3(20, 10, 12);
  27.     }
  28.  
  29.      char ***matrix3(int row, int col, int cell)
  30.      {
  31.          int i=0,j=0;
  32.          char ***tmp;
  33.    
  34.          tmp=(char ***)malloc(row * col * sizeof(char **)); 
  35.          for (i=0;i<row;i++)
  36.               {
  37.                   tmp[i]=(char **)malloc(row * sizeof(char *));
  38.                   for (j=0;j<col;j++)
  39.               tmp[i][j]=(char *)malloc(cell * sizeof(char));
  40.               }
  41.      return(tmp);
  42.      }
  43.  
  44. Because it is a 3 dimensional pointer, I must malloc each level separately.
  45. Attempting a realloc using the same format does not work:
  46.  
  47.     void rematrix3(char ***tmp, int row, int col, int cell)
  48.     {
  49.         int i=0,j=0;
  50.    
  51.         tm=(char ***)realloc(tmp, row * col * sizeof(char **));
  52.         for(i=0;i<row;i++)
  53.         {
  54.             tmp[i]=(char **)realloc(tmp[i], row * sizeof(char *));
  55.             for(j=0;j<col;j++)
  56.                 tmp[i][j]=(char *)realloc(tmp[i][j], cell * sizeofchar));
  57.         }
  58.    
  59.     return;      
  60.     }
  61.  
  62. -- Via DLG Pro v0.992
  63.  
  64.