pointer confusion.

R

Ryan Knopp

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 */
}
 
W

William Pursell

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.
*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.
 
R

Ryan Knopp

William said:
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!.
 
U

user923005

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/
 
B

Ben Bacarisse

Ryan Knopp said:
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.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top