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

    Join Date
    May 2003
    Posts
    24
    Rep Power
    0

    realloc and pointers are driving me crazy!!


    I have this code, and the problem is that every time I add a new element to my big list, i keep overwriting what I had before, and the tail never moves to the end of the list...........does anyone know what I am doing wrong?? Please help because I have looked at this thing too long, and I can't figure it out...it might be really simple.......thanks very much in advance...............


    #include <stdlib.h>

    typedef struct{
    int num;
    int val;
    }blah;

    void printBig(blah * start, blah * end, int size); //prints a list of blah variables
    void printOne(blah T); //prints one blah varaible

    int main()
    {
    blah * big=NULL; //head of list of blahs
    blah * bigTail; //tail of list of blahs
    blah * bigptr; //another pointer
    int i=0;
    int bigsize=0; //total size of big
    int onesize=sizeof(blah); //size of one blah
    blah temp;

    while(i<3)
    {
    temp.num = i+10;
    temp.val = i+10;
    bigsize += onesize; //recalculate size of big
    big = realloc(big, bigsize); //allocate that much space for the big

    bigTail = big + bigsize - onesize; //make tail point to end of list - size of one
    //blah
    memcpy(bigTail, &temp, onesize); //copy this blah to list
    bigTail += onesize; //adjust tail
    i++;
    }

    printBig(big, bigTail, onesize); //print list
    return 0;
    }

    void printBig(blah * start, blah * end, int size)
    {
    int i;
    blah * head = start;
    blah * tail = end;

    printf("{ \n");
    while(start!=end)
    {
    printOne(*start);
    start+=size;
    }

    printf(" }\n");
    }

    void printOne(blah T)
    {
    int i;
    printf("(%d, %d) ", T.num, T.val);
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    The problem lies in your pointer math. In the following line, you are actually adding (bigsize * sizeof (blah)) and then subtracting (onesize * sizeof (blah)). The compiler knows that you are working with a pointer to a blah so it assumes that increments and decrements of that pointer are in sizeof (blah) units.

    instead of
    Code:
    bigTail = big + bigsize - onesize; //make tail point to end of list - size of one
    try

    Code:
    bigtail = big + (bigsize/onesize) - 1; //make tail point to end of list - size of one
    You will also need to make a similar change to the following line:

    change
    Code:
    bigtail += onesize;  // adjust tail
    to
    Code:
    bigtail++;
    I almost forgot one. You don't need to pass the size into the printBig function. You can just change

    Code:
    start += size;
    to

    Code:
    start++;
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    24
    Rep Power
    0

    Thank you soooo much


    I don't know how to thank you enough.......Your explanation was wonderful! Hehehe, and I hate pointers to death.....thanks again, you have helped so much!

    Kavi

IMN logo majestic logo threadwatch logo seochat tools logo