pointer confusion.

Discussion in 'C Programming' started by Ryan Knopp, Dec 11, 2007.

  1. Ryan Knopp

    Ryan Knopp Guest

    I'm having some bizarre problems with some of my memory allocation.
    I've ran it threw gdb and noticing my memory indexes aren't matching up.

    here's an example. I have commented within the code.

    static void getvideos(xmlTextReaderPtr, struct video**, int*);
    static struct video *getvideo(xmlTextReaderPtr);
    struct video **loadconfig(const char const *filename)
    {
    xmlTextReaderPtr reader;
    struct video *videos = NULL;
    int ret, numVid = 0;

    reader = xmlReaderForFile(filename, NULL, 0);
    if(reader == NULL){
    perror("Unable to open ");
    return NULL;
    }

    ret = xmlTextReaderRead(reader);
    if(!ret)
    perror("xml problem");

    while(ret == 1)
    {
    if((strncasecmp("videos", xmlTextReaderConstName(reader), 6) == 0)
    && xmlTextReaderNodeType(reader) == 1)
    getvideos(reader, &videos, &numVid);

    ret = xmlTextReaderRead(reader);
    }

    /* when i get here, the first index of the array is there but the
    rest of the indexes are fragmented. */

    xmlCleanupParser();
    }
    static void getvideos(xmlTextReaderPtr reader, struct video **videos,
    int *numVid)
    {
    int ret = 1;
    struct video *tmp;

    while((ret == 1) && !((xmlTextReaderNodeType(reader) == 15) &&
    (strncasecmp("videos", xmlTextReaderConstName(reader), 6
    {
    if((xmlTextReaderNodeType(reader) == 1) && (strncasecmp("video",
    xmlTextReaderConstName(reader), 5)))
    {
    *videos = realloc(*videos, ++(*numVid) * sizeof(struct video *));
    tmp = getvideo(reader);
    if(tmp != NULL)
    videos[(*numVid)-1] = tmp;
    tmp = NULL;
    }
    ret = xmlTextReaderRead(reader);
    }
    return; /* the data is perfect at this part of the execution */
    }
    Ryan Knopp, Dec 11, 2007
    #1
    1. Advertising

  2. On Dec 11, 11:27 pm, Ryan Knopp <> wrote:
    > I'm having some bizarre problems with some of my memory allocation.
    > I've ran it threw gdb and noticing my memory indexes aren't matching up.
    >
    > here's an example. I have commented within the code.

    <snip>
    > *videos = realloc(*videos, ++(*numVid) * sizeof(struct video *));


    Ack! What happens when realloc fails? More importantly,
    you got the size wrong. That should be
    "sizeof **videos" or "sizeof( struct video)".
    Stylistically, the former is preferred, almost
    precisely to avoid this mistake.
    William Pursell, Dec 12, 2007
    #2
    1. Advertising

  3. Ryan Knopp

    Ryan Knopp Guest

    William Pursell wrote:
    > On Dec 11, 11:27 pm, Ryan Knopp <> wrote:
    >> I'm having some bizarre problems with some of my memory allocation.
    >> I've ran it threw gdb and noticing my memory indexes aren't matching up.
    >>
    >> here's an example. I have commented within the code.

    > <snip>
    >> *videos = realloc(*videos, ++(*numVid) * sizeof(struct video *));

    >
    > Ack! What happens when realloc fails? More importantly,
    > you got the size wrong. That should be
    > "sizeof **videos" or "sizeof( struct video)".
    > Stylistically, the former is preferred, almost
    > precisely to avoid this mistake.
    >
    >

    Thanks for your help, but that didn't completely solve my problem.
    The memory indexes still aren't matching up.

    <snip>
    ret = xmlTextReaderRead(reader);
    }
    return; /* the data is perfect at this part of the execution */
    }
    </snip> This returns perfectly and according to gdb has the memory
    references of:
    (gdb) print videos[0]
    $19 = (struct video *) 0x8059668
    (gdb) print videos[1]
    $20 = (struct video *) 0x8059600
    (gdb) print videos[2]
    $21 = (struct video *) 0x80591a8

    When I continue on and it returns back to the original function

    <snip>
    xmlCleanupParser(); /* here */
    </snip>
    The memory address for this is
    (gdb) print &videos[0]
    $24 = (struct video *) 0x8059668
    (gdb) print &videos[1]
    $25 = (struct video *) 0x8059670
    (gdb) print &videos[2]
    $26 = (struct video *) 0x8059678


    I don't understand why they aren't the same. It seems like the pointer
    declaration and dereferencing are all happening correctly.

    Any thoughts? Thanks for your help!.
    Ryan Knopp, Dec 12, 2007
    #3
  4. Ryan Knopp

    user923005 Guest

    On Dec 11, 8:49 pm, Ryan Knopp <"ryan <---deletethis--> wrote:
    > William Pursell wrote:
    > > On Dec 11, 11:27 pm, Ryan Knopp <> wrote:
    > >> I'm having some bizarre problems with some of my memory allocation.
    > >> I've ran it threw gdb and noticing my memory indexes aren't matching up.

    >
    > >> here's an example. I have commented within the code.

    > > <snip>
    > >> *videos = realloc(*videos, ++(*numVid) * sizeof(struct video *));

    >
    > > Ack! What happens when realloc fails? More importantly,
    > > you got the size wrong. That should be
    > > "sizeof **videos" or "sizeof( struct video)".
    > > Stylistically, the former is preferred, almost
    > > precisely to avoid this mistake.

    >
    > Thanks for your help, but that didn't completely solve my problem.
    > The memory indexes still aren't matching up.
    >
    > <snip>
    > ret = xmlTextReaderRead(reader);
    > }
    > return; /* the data is perfect at this part of the execution */}
    >
    > </snip> This returns perfectly and according to gdb has the memory
    > references of:
    > (gdb) print videos[0]
    > $19 = (struct video *) 0x8059668
    > (gdb) print videos[1]
    > $20 = (struct video *) 0x8059600
    > (gdb) print videos[2]
    > $21 = (struct video *) 0x80591a8
    >
    > When I continue on and it returns back to the original function
    >
    > <snip>
    > xmlCleanupParser(); /* here */
    > </snip>
    > The memory address for this is
    > (gdb) print &videos[0]
    > $24 = (struct video *) 0x8059668
    > (gdb) print &videos[1]
    > $25 = (struct video *) 0x8059670
    > (gdb) print &videos[2]
    > $26 = (struct video *) 0x8059678
    >
    > I don't understand why they aren't the same. It seems like the pointer
    > declaration and dereferencing are all happening correctly.


    One of two things is happening:
    1. You are moving the addresses via pointer math without saving the
    originals.
    2. You are overwriting the addresses with a memcopy or some array out
    of bounds or something naughty like that.

    I suggest trying the Duma library.
    http://duma.sourceforge.net/
    user923005, Dec 12, 2007
    #4
  5. Ryan Knopp <> writes:

    > I'm having some bizarre problems with some of my memory
    > allocation. I've ran it threw gdb and noticing my memory indexes
    > aren't matching up.

    <snip>
    > static void getvideos(xmlTextReaderPtr reader, struct video **videos,
    > int *numVid)
    > {
    > int ret = 1;
    > struct video *tmp;
    >
    > while((ret == 1) && !((xmlTextReaderNodeType(reader) == 15) &&
    > (strncasecmp("videos", xmlTextReaderConstName(reader), 6
    > {
    > if((xmlTextReaderNodeType(reader) == 1) && (strncasecmp("video",
    > xmlTextReaderConstName(reader), 5)))
    > {
    > *videos = realloc(*videos, ++(*numVid) * sizeof(struct video *));
    > tmp = getvideo(reader);
    > if(tmp != NULL)
    > videos[(*numVid)-1] = tmp;


    You have to decide on what type things are. I suspect getvideo
    returns a 'struct video *'. These (pointers) are stored in an array
    whose first element is pointed to by a 'struct video **'. I think you
    want this function to allocate that array of pointers. To do that you
    need to pass it a 'struct video ***'. You are a * short.

    > tmp = NULL;
    > }
    > ret = xmlTextReaderRead(reader);
    > }
    > return; /* the data is perfect at this part of the execution */
    > }


    --
    Ben.
    Ben Bacarisse, Dec 12, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    676
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    827
    Jordan Abel
    Mar 28, 2006
  3. Kavya
    Replies:
    2
    Views:
    370
  4. Replies:
    4
    Views:
    1,234
    Fred Zwarts
    Jul 2, 2009
  5. A
    Replies:
    7
    Views:
    626
Loading...

Share This Page