realloc questions

Peter C. Bahrs pcb at gator.cacs.usl.edu
Tue Feb 27 06:36:02 AEST 1990


This may be a naive discussion but here goes...

I want to create an N (2 for now) dimensional datum of long integers.

    typedef struct a 
            { int row, col; long **arr} A;
    A *temp;

    temp=(A *) calloc (1, sizeof(A));  /* then initialize row and col ... */
    A->arr=(long *)calloc (A->row, sizeof(long *));
    for (j=0;j<A->row;j++)
       A->arr[j] = calloc(col,sizeof(long));

1) I should now be able to index as A->arr[j][k], correct?
2) or should type A contain long *arr[] instead?

Now I want to redimension the type to a new_row and new_col. 

   A->arr=(long *) realloc ((char *)&A->arr, new_row*sizeof(long *));
   for (j=0;j<A->new_row;j++)
       A->arr[j] = realloc(new_col,sizeof(long));
  
3) This doesn't work (sometimes).  I understand realloc will retain values
   of the minimum of the old and new dimensions, and 'frees' memory if
   the new is smaller.  
   
Any suggestions? 

/*----------- Thanks in advance... --------------------------------------+
| Peter C. Bahrs                                                         |
| The USL-NASA Project                                                   |
| Center For Advanced Computer Studies      INET: pcb at gator.cacs.usl.edu |
| 2 Rex Street                                                           |
| University of Southwestern Louisiana      ...!uunet!dalsqnt!gator!pcb  | 
| Lafayette, LA 70504                                                    |
+-----------------------------------------------------------------------*/



More information about the Comp.lang.c mailing list