#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    24
    Rep Power
    0

    ONE more question about Pointer math --> 3dfxMM


    Sorry this is another post about my last question titled "realloc and pointers are driving me crazy"
    3dfxMM, thanks for ur help the first time, can you help me again please?


    This time, 'fool' has an array of 'blahs' and the program results in 3 copies of the last 'fool' generated...sorry about my variable names, I was just getting a little frustrated.....

    The problem is that I cannot increment a pointer to fool properly because a 'fool' has as a memeber, an array that needs to be dynamically allocated. If i fix the size of blahAr as 2, then my code works perfectly...BUT my actual design is such that I have to make it dynamic. . Does anyone know how I can make it such that I can increment my pointer properly??

    thanks in advance for any input...


    Code:
    #include <stdlib.h>
    
    typedef struct{
      int num;
      int val;
    }blah;
    
    typedef struct{
      int nonsense;  
      blah * blahAr;
    }fool;
    
    void printBig(fool * start, fool * end);
    void printOne(fool T); 
    
    int main()
    {
       fool * big=NULL;
       fool * bigTail; 
       fool * bigptr;
       int i=0,j=0; 
       int bigsize=0;
       int onesize;
       blah temp; 
    
       fool foolish;
       foolish.blahAr = malloc(sizeof(blah)*2);
       onesize = sizeof(blah)*2 + sizeof(int);
    
       while(i<3)
       {
          temp.num = i+15;
          temp.val = i+15;
          foolish.nonsense=i;
    
          for(j=0; j<2; j++)
         {
             temp.num++;
             temp.val++;
             foolish.blahAr[j] = temp;
         }
    
         bigsize += onesize; //recalculate size of big
         big = realloc(big, bigsize); 
         bigTail = big + bigsize/onesize - 1;  
         memcpy(bigTail, &foolish, onesize); 
         bigTail++; //adjust tail
         i++; 
         printBig(big, bigTail); //print list
      }
    
       printBig(big, bigTail); //print list
       return 0;
    }
    
    void printBig(fool * start, fool * end, int size)
    {
       int i;
       fool * head = start;
       fool * tail = end;
    
       printf("{ \n");
       while(start!=end)
      {
         printOne(*start);
         start++;
      }
    
       printf(" }\n"); 
    }
    
    void printOne(fool T)
    {
        int i=0;
        printf("[ ");
        for(i=0;i<2;i++)
           printf("(%d, %d) ", T.blahAr[i].num, T.blahAr[i].val);
        printf(" ]");
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Pointers only come in one size, regardless of the size of what they are pointing at[*]. The pointer, blahAr, is contained within the struct, fool, and is of size sizeof(blah*) [ which == sizeof(void*), which == sizeof(char*), which == sizeof(double*), etc]. So the size of the struct, fool, will be the same regardless of what blahAr points to.

    Remember, when you dynamically allocate space for blahAr[], that block of memory gets allocated out in the heap somewhere; it does not get allocated inside of fool.

    Footnote:[*] Strictly speaking that is only true as long as you stick exclusively to the same kind of pointer; e.g. near pointers vs far pointers. In your case, you are sticking with near pointers, so the rule applies.
    Last edited by dwise1_aol; May 30th, 2003 at 06:25 PM.
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

    Re: ONE more question about Pointer math --> 3dfxMM


    Originally posted by kavi_s
    .sorry about my variable names, I was just getting a little frustrated.....
    heh, i know what u mean! LOL! i give my functions/variables all sorts of derogatory names, and often my comments will be littered with my outbursts of frustration:D
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222

    Re: Re: ONE more question about Pointer math --> 3dfxMM


    Originally posted by infamous41md
    heh, i know what u mean! LOL! i give my functions/variables all sorts of derogatory names, and often my comments will be littered with my outbursts of frustration:D
    As do many others. Some of it survives in error messages. Just yesterday, for the first time in Red Hat 7 I messed up on the tar command for creating an archive and failed to give it any files to place into the archive. The error message was something like "tar cowardly refuses to create an empty archive."
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    24
    Rep Power
    0

    Thanks


    Thanks, dwise1_aol, for ur help....I need to make some major changes to my code :S

IMN logo majestic logo threadwatch logo seochat tools logo