Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUDs ➜ General ➜ pointers within a loop, maybe an easy problem

pointers within a loop, maybe an easy problem

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Trom   (82 posts)  Bio
Date Fri 09 May 2008 05:05 PM (UTC)

Amended on Fri 09 May 2008 05:06 PM (UTC) by Trom

Message
I've never learned c language from books or anything so this maybe a simple problem.


//QUESTLIST_DATA *quest_list; its global
QUESTLIST_DATA *ql;
word = fread_word( fp );
if ( !str_cmp(word,"QL") || !str_cmp(word,"QUESTLIST") ) {
    ql = new_ql();
    fread_ql ( ql, fp );
    if ( quest_list != NULL )
        ql->next = quest_list;
    quest_list = ql;
    free_ql(ql);
}
else if ( !str_cmp( word, "END"    ) )
    break;


What does the above code do?
----------------------------
The above is the code i have within a loop. The problem is somewhat obvious. Memory is allocated to 'ql' before its used and then its filled with values, then added to the main global 'quest_list'. 'ql' was supposed to be free'd after the values were passed to the global 'quest_list', but instead it clears part of the 'quest_list' becuase 'ql' still retains the reference.

What I have tried
-----------------
Now i didn't want to have it use new_ql over and over without free'ing it since that would probably be bad for memory use. I also tried before having it allocate memory to ql once (before the loop begins) and i've also tried not having a free_ql at all. (the end result was faulty, users would lockup the mud when they connect.. the quest_list produces infinite loops somehow).

To The Point
------------
What i was hoping to do is free_ql() but not losing the values, just killing the reference so 'ql' can be used when the loop reaches its beginning again. I'm unsure of how to do that. Any help would be appreciated :)
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 09 May 2008 08:59 PM (UTC)
Message
This code doesn't work, right? It looks sort of strange.

For example, even if ql is NULL, you are freeing it.

What does fread_ql do? Does it also allocate the pointer?

Quote:

Now i didn't want to have it use new_ql over and over without free'ing it since that would probably be bad for memory use.


Without knowing what fread_ql does, it is perfectly normal to allocate memory for something like a list of quests and not free it. After all, they have to take memory somewhere.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Trom   (82 posts)  Bio
Date Reply #2 on Sat 10 May 2008 05:10 AM (UTC)

Amended on Sat 10 May 2008 05:16 AM (UTC) by Trom

Message
Sorry for not being clear. I followed the use of free_char new_char and fread_char.

new_ql() - returns an allocated new ql, ready for use.
fread_ql() - reads the values from the actual file and stores it in the ql specified within the argument.
free_ql() - places the ql from the arguments in a global deadspace list. new_ql checks the deadspace list before allocating another to see if any existing structure is available (thats already taking up memory but not in use).

I've made a quest editor and the quest is held within quest_list. I named the nodes within the quest_list, 'ql'. Theres a way with the quest editor to remove an entry by making the id 0. When new_ql is called, it first searches the global quest_list and finds an entry thats id == 0. Then returns that instead of allocating another (then it searches deadspace, then finally if both are not available, it creates a new allocation).

ql is basically a temp structure. I was hoping to just assign the node to the global quest_list, then get rid of ql since it would linger.

The problem remains though that when reading two entries from a file (two linklist nodes), the first one remains, no other.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Sat 10 May 2008 05:40 AM (UTC)
Message
You allocate memory for the quest, place it in the linked list, and then free the same pointer. I am not surprised it is not still there.

Anyway, I think I would amend it like this:


if ( !str_cmp(word,"QL") || !str_cmp(word,"QUESTLIST") ) {
    ql = new_ql();
    fread_ql ( ql, fp );
    ql->next = quest_list;
    quest_list = ql;
}


You are obviously adding to the head of the list, so you need to add the existing list to ql->next (even if the existing list is NULL). Then the new item becomes the new list (head). I wouldn't free the quests, that would certainly crash something.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Trom   (82 posts)  Bio
Date Reply #4 on Sat 10 May 2008 06:05 AM (UTC)
Message
Thank you again for all the help. It is now working fully :)
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,535 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.