473,395 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

pointer confusion.

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 */
}
Dec 11 '07 #1
4 1955
On Dec 11, 11:27 pm, Ryan Knopp <ryan--nospa...@theknopps.comwrote:
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.
Dec 12 '07 #2
William Pursell wrote:
On Dec 11, 11:27 pm, Ryan Knopp <ryan--nospa...@theknopps.comwrote:
>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!.
Dec 12 '07 #3
On Dec 11, 8:49 pm, Ryan Knopp <"ryan <---deletethis--wrote:
William Pursell wrote:
On Dec 11, 11:27 pm, Ryan Knopp <ryan--nospa...@theknopps.comwrote:
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/
Dec 12 '07 #4
Ryan Knopp <ry************@theknopps.comwrites:
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.
Dec 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

69
by: Ken | last post by:
Hi all. When referring to a null pointer constant in C++, is there any reason to prefer using 0 over a macro called NULL that is defined to be 0? Thanks! Ken
1
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
9
by: Koster | last post by:
Something I've been puzzled over for a while. This really has no consequence at all, but I'm curious for the _reason_ behind where we commonly place the asterisk when declaring pointer variables....
3
by: randomtalk | last post by:
hello everyone! Well, recently i've been trying to pick up c and see what is pointer all about (been programming in lisp/python for the better part of my last two years).. mmm.. I'm currently...
3
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
6
by: Scott | last post by:
Hi All, I can't seem to wrap my head around this one. I have a pointer, int *x; which I can assign:
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
17
by: matevzb | last post by:
I've ran into some fishy code that, at first glance, is buggy, but it seems to work correctly and none of the compilers I've tried (five so far, on various systems) gives any warnings. The code:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.