home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!amusing!grace!Douglas_Stockman
- From: Douglas_Stockman@grace.UUCP (Douglas Stockman)
- Newsgroups: comp.sys.amiga.programmer
- Subject: HELP - malloc, realloc
- Distribution: world
- Message-ID: <Douglas_Stockman.1lyc@grace.UUCP>
- Date: 18 Dec 92 09:39:21 EST
- Organization: Graceful Boot BBS (send replies to grace!User_Name@moscom.com)
- Lines: 53
-
- [[AAIn response to jbickers@templar.actrix.gen.nz (John Bickers)'s message:
- > You should be able to get away with:
- >
- > ptr = (cast)realloc(ptr,newsize);
- >
- > What goes wrong with that?
-
-
- I may be missing something, but the answer seems more complicated to me.
- To perform the initial malloc I use something like the following:
-
- char ***temp; /** equivalent to temp[row][col][cell] **/
-
- main
- {
- temp = matrix3(20, 10, 12);
- }
-
- char ***matrix3(int row, int col, int cell)
- {
- int i=0,j=0;
- char ***tmp;
-
- tmp=(char ***)malloc(row * col * sizeof(char **));
- for (i=0;i<row;i++)
- {
- tmp[i]=(char **)malloc(row * sizeof(char *));
- for (j=0;j<col;j++)
- tmp[i][j]=(char *)malloc(cell * sizeof(char));
- }
- return(tmp);
- }
-
- Because it is a 3 dimensional pointer, I must malloc each level separately.
- Attempting a realloc using the same format does not work:
-
- void rematrix3(char ***tmp, int row, int col, int cell)
- {
- int i=0,j=0;
-
- tm=(char ***)realloc(tmp, row * col * sizeof(char **));
- for(i=0;i<row;i++)
- {
- tmp[i]=(char **)realloc(tmp[i], row * sizeof(char *));
- for(j=0;j<col;j++)
- tmp[i][j]=(char *)realloc(tmp[i][j], cell * sizeofchar));
- }
-
- return;
- }
-
- -- Via DLG Pro v0.992
-
-