LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2018, 06:32 AM   #1
tarika
LQ Newbie
 
Registered: Sep 2018
Posts: 7

Rep: Reputation: Disabled
Linked List with a pointer to structs


I have two libraries: one with a linked list of a pointer of structs and its operations, and the other with a xmlParser that fills the linked list.
An example of the xml that is going to be read has the following form:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<AuthorisedDealer_Input>
   <Yamaha>
      <car model="Motiv" description="city car" currency="euros">30000</car>
      <car model="OX99-11" description="sports car" currency="bitcoin">5</car>
   </Yamaha>
   <Honda>
      <car model="Civic" description="private car" currency="pounds">20520</car>
      <car model="Jazz" description="little car" currency="pounds">19000</car>
   </Honda>
   <Scorpion>
      <car model="Audi" description="smart car" currency="dolars">50000</car>
   </Scorpion>
</AuthorisedDealer_Input>
My linked list is the following:
Code:
struct Car{
	xmlChar *model;
	xmlChar *description;
	xmlChar *currency;
	xmlChar *price;
};

struct AuthorisedDealer {
	const xmlChar *name;
	struct Car* cars;
	struct AuthorisedDealer* next;
};
Its functions are the following:
Code:
struct AuthorisedDealer* SortedMerge(struct AuthorisedDealer* a, struct AuthorisedDealer* b)
{
    struct AuthorisedDealer dummy;

    struct AuthorisedDealer* tail = &dummy;

    dummy.next = NULL;
    while (1)
    {
        if (a == NULL)
        {
            tail->next = b;
            break;
        }
        else if (b == NULL)
        {
            tail->next = a;
            break;
        }
        if(xmlStrcmp(a->model, b->model)<=0)
            MoveAuthorisedDealer(&(tail->next), &a);
        else
            MoveAuthorisedDealer(&(tail->next), &b);

        tail = tail->next;
    }
    return(dummy.next);
}

void MoveAuthorisedDealer(struct AuthorisedDealer** destRef, struct AuthorisedDealer** sourceRef)
{
    /* the front source node  */
    struct AuthorisedDealer* newNode = *sourceRef;

    /* Advance the source pointer */
    *sourceRef = newNode->next;

    /* Link the old dest off the new node */
    newNode->next = *destRef;

    /* Move dest to point to the new node */
    *destRef = newNode;
}

void push(struct AuthorisedDealer** head_ref, xmlChar *new_name, struct Car* new_cars)
{
    struct AuthorisedDealer* new_node = (struct AuthorisedDealer*) malloc(sizeof(struct AuthorisedDealer));

    new_node->name  = new_name;
    new_node->cars  = new_cars;

    new_node->next = (*head_ref);

    (*head_ref)    = new_node;
}

void pushData(struct AuthorisedDealer** head_ref, const xmlChar *new_name, xmlNode *cur_node)
{
    struct AuthorisedDealer* new_node = (struct AuthorisedDealer*) malloc(sizeof(struct AuthorisedDealer));

    new_node->name  = new_name;

    new_node->next = (*head_ref);

    (*head_ref)    = new_node;
}

void updateParams(struct AuthorisedDealer *ps, xmlNode *cur_node) {
	struct Car* new_node = (struct Car*) malloc(sizeof(struct Car));
	new_node->model = xmlGetProp(cur_node, (const xmlChar *)"model");
	new_node->description = xmlGetProp(cur_node, (const xmlChar *)"description");
	new_node->currency = xmlGetProp(cur_node, (const xmlChar *)"currency");
	new_node->price = cur_node->children->content;
	ps->params = new_node;
}

/* Function to print nodes in a given linked list */
void printList(struct AuthorisedDealer *node)
{
    while (node!=NULL)
    {
        printf("%s ", node->name);
        node = node->next;
    }
}
And the xml parser that reads the xml file at the same time that fills this data structure is the following:
Code:
struct AuthorisedDealer *XMLparser(char *pathName) {

	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;

	doc = xmlReadFile(pathName, NULL, 0);

	root_element = xmlDocGetRootElement(doc);

	int len = strlen(pathName);
	char tmpPathName[len];
	strcpy(tmpPathName,pathName);
	tmpPathName[len-4]='\0';
	char * rootString;
	int ch = '/';
	rootString = strrchr(tmpPathName, ch);
	rootString++;
	int row = 0;
	struct AuthorisedDealer *ps = print_element_names(row, root_element, rootString);

	xmlCleanupParser();
	return ps;
}

struct AuthorisedDealer *print_element_names(int row, xmlNode *a_node, char *rootString)
{
	struct AuthorisedDealer *ps;
    xmlNode *cur_node = NULL;
    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        if (cur_node->type == XML_ELEMENT_NODE) {
        	if ((xmlStrcmp(cur_node->name, (const xmlChar *)"car"))) {
        		if (xmlStrcmp(cur_node->name, (const xmlChar *)rootString) ) {
        				pushData(&ps, cur_node->name, cur_node);
        				printf("Element, name: %s\n", cur_node->name);
        		} 
        	} else {
				printf("model: %s\n", xmlGetProp(cur_node, (const xmlChar *)"model"));
        		updateParams(ps, cur_node);	//it fails here
        	}
        }
        if(xmlChildElementCount(cur_node)>0) {
        	struct AuthorisedDealer *secondPs = print_element_names(row, cur_node->children, rootString);
        	ps = SortedMerge(ps, secondPs);
        }
    }
    return ps;
}
I have checked that the xml parser is ok, my problem is filling the data structure, I don't understand why it fails because there isn't any error message, but in the call updateParams(ps, cur_node); it stops working.
Thanks in advance.

Last edited by tarika; 09-26-2018 at 06:22 AM.
 
Old 09-17-2018, 08:04 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Welcome to LQ.

Please use [code] tags to enclose any future code, it will make it far more readable.

There's a link in my signature which describes how to do this.

struct Car contains four pointers.

When you allocate a struct Car, it allocates the size required for those pointers, but does not initiate them, nor does it allocate space for each of those attributes. This is why you have one error.
 
1 members found this post helpful.
Old 09-18-2018, 02:36 AM   #3
tarika
LQ Newbie
 
Registered: Sep 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks, Rtmistler, but I don't find the link "struct Car contains four pointers", could you send me the link? I am not sure what I have to do.
 
Old 09-18-2018, 06:10 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by tarika View Post
Thanks, Rtmistler, but I don't find the link "struct Car contains four pointers", could you send me the link? I am not sure what I have to do.
Well you seemed to have found out how to place code in the proper tags. Thank you for editing.


Unsure if you feel there was some other link. My comment was that struct Car contains four pointers. Here is a cut/paste of the code you posted containing that structure:
Code:
struct Car{
     xmlChar *model;
     xmlChar *description;
     xmlChar *currency;
     xmlChar *price;
 };
This structure contains four pointers. Therefore when you allocate space for this structure, you only allocate space for the four pointers and not space for the xmlChar strings which they are to contain.
 
1 members found this post helpful.
Old 09-18-2018, 07:25 AM   #5
tarika
LQ Newbie
 
Registered: Sep 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
When I allocate space for this structure I use this line of code:
struct Car* new_node = (struct Car*) malloc(sizeof(struct Car));
Car already includes four pointers to xmlChar, and a pointer to a xmlChar is a xmlChar string. I am not sure what you mean.
 
Old 09-18-2018, 07:40 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Perhaps I am incorrectly focusing on that and thinking it becomes an error.

Yes. A pointer to an xmlChar is a pointer to a string.

However, a Non-Allocated string.

When you allocate your structure, you solely allocate the pointers alone and not the memory which the pointers are supposed to contain.

Nor are you initializing these pointers.

Is an xmlGetProp() call returning the value of a valid string that is already allocated? If so, then my concerns are unwarranted, and you are only assigning valid values to these pointers.

Therefore what else can go wrong within updateParams()?

Have you verified that the passing argument 'ps' is a valid address?

Is cur_node a valid address?

Have you verified that the call to malloc() worked correctly?

What possible error returns could you get from xmlGetProp()?
 
1 members found this post helpful.
Old 09-18-2018, 08:22 AM   #7
tarika
LQ Newbie
 
Registered: Sep 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes, I have checked xmlGetProp return a valid xmlChar*, not an error. I have check that the new_node contains the parameters of the car (in this case model "Motiv", description "city car", currency "euros" and price "30000").
When I debug updateParams it stops working in the following line of code:
ps->params = new_node;
How I can verify ps is a valid address?
cur_node is a valid address and it contains the xmlNode* with the content of the xml file.
 
Old 09-18-2018, 08:34 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Wait a minute.

ps is an AuthorisedDealer structure:
Code:
struct AuthorisedDealer {
	const xmlChar *name;
	struct Car* cars;
	struct AuthorisedDealer* next;
};
As used in your updateParams() function:
Code:
void updateParams(struct AuthorisedDealer *ps, xmlNode *cur_node) {
    ...
    ps->params = new_node;
How does this code compile?

An AuthorisedDealer structure does NOT have a params member.
 
1 members found this post helpful.
Old 09-18-2018, 08:44 AM   #9
tarika
LQ Newbie
 
Registered: Sep 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
I have misspelled. I mean:

Code:
void updateParams(struct AuthorisedDealer *ps, xmlNode *cur_node) {
    ...
    ps->cars = new_node;
It fails in ps->cars = new_node;
for this reason, it compiles.
 
Old 09-18-2018, 09:02 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
What's the failure you're seeing?
 
1 members found this post helpful.
Old 09-18-2018, 09:19 AM   #11
tarika
LQ Newbie
 
Registered: Sep 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
I am compiling and debuging with Eclipse and it does not show any error, neither warning. But the execution is finished in this line of code.
 
Old 09-18-2018, 09:26 AM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by tarika View Post
I am compiling and debuging with Eclipse and it does not show any error, neither warning. But the execution is finished in this line of code.
I'm at a loss to understand how you know this is the point where there is an error, or that it stops here.


Exactly how have you debugged it to reach this conclusion?
 
1 members found this post helpful.
Old 09-18-2018, 09:38 AM   #13
tarika
LQ Newbie
 
Registered: Sep 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
I have debugged with Eclipse line by line and in this line it stops.
 
Old 09-18-2018, 10:34 AM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Have all you done so far is to single step and verified that it fails and not analyzed any classification or diagnosis of the failure?

Such as just before this line of execution, have you verified all pointers, variables, and data are valid?
 
1 members found this post helpful.
Old 09-19-2018, 02:06 AM   #15
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Sorry to be so late getting into the discussion here, and thanks RT for carrying the ball this far!

@tarika - You need to be more complete and precise in telling us exactly what this entails...

Quote:
I have debugged with Eclipse line by line and in this line it stops.
If as asked by rtmistler you are single stepping through the code, and it fails at this line, then have you checked the pointers and variables individually immediately before it "stops"? And what do you mean it stops - does the program just exit "normally", directly from that line? How so? Does it return any error messages or status indication at all?

We want to help, but only you are sitting at the compiler and it is you who must debug the code. We can offer suggestions of methods for troubleshooting it, but we must know what is actually happening - only you can tell us that!

Please pause and consider how best to explain to us how you are compiling and running the code, and what exactly is happening that makes you think this line of code is responsible. And if you are certain that this line of code is responsible then tell us exactly what you have done to verify all the pointers and data state immediately before it fails. Please do so with actual results of your tests, as opposed to simply saying yes they are all OK.

For a very good guide to asking complete questions, please review the LQ FAQs page, and read the linked article at bottom of that page, How to ask questions the smart way.

Help us help you!
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked list C exvor Programming 14 06-22-2007 06:06 PM
Linked list manas_sem Programming 3 12-21-2006 01:53 AM
GTK code uses pointer for structs and not normal instances why? anksphenomenon Linux - Desktop 0 10-23-2006 04:45 AM
C linked list exvor Programming 4 04-28-2006 05:25 AM
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration