The Dawn Of Time
The Official Forums for the Dawn Of Time Mud Codebase
 
Log in Register FAQ Memberlist Search The Dawn Of Time Forum Index Goto the Official Dawn Website

Linked Struct List Gone Wrong

 
Post new topic   Reply to topic    The Dawn Of Time Forum Index » Coding
View previous topic :: View next topic  
Author Message
Oreant



Joined: 16 Jul 2010
Posts: 47

PostPosted: Fri Jan 22, 2016 4:48 pm    Post subject: Linked Struct List Gone Wrong Reply with quote

I've managed to run myself in circles and make an absolute cluster out of what should have been a rather straight forward linked set of structs.

The objective of the code is to save a spell in a list with certain selected properties and a name. Something of a dynamic system for creating specialized spells. The initial error was after the 5th item was added, it would start overwriting the names of the original item (while still adding the new one). Basically: ABCDE would go to FGCDEFG when you add two more. They would have the correct additional properties, but the name would be trashed. And the command to remove one.. now blows up (at least it does now).

Any help or direction getting me back on course would be appreciated. I attempted to go back and mimic others I saw and yet I'm still doing something badly wrong.

Main function:
Code:

void do_prepare(char_data * ch, char * argument)
{
   char name[MIL];
   char spellprint[MIL];
   char property[MIL];
   int prop;

   int spellcount = 0;
   RETAINED_SPELL *rs;//, *rs_next;
   
   for(rs = ch->retain_first; rs; rs = rs->next)
   {
      if( !str_prefix( name, rs->name) ){
         ch->printlnf("You have already prepared something called '%s'.", rs->name);
         return;
      }
      spellcount++;
   }

   long      spellProperties = 0;
   long      spellPrintID = -1;

   if( (spellPrintID = spellprint_lookup(spellprint))  <  0){
      ch->printlnf("Spellprint (%s) not found.",spellprint);
      return;
   }
   while ( !IS_NULLSTR(property) ){
      if( (prop = property_lookup(property))  <  0){
         ch->printlnf("'%s' is an invalid spell property.",property);
      } else {
         SET_BIT(spellProperties, prop);
      }
         
      argument = one_argument( argument, property );
   }
   
   if ( spellProperties == 0 ){
      ch->println("No valid properties selected.");
      return;
   }
   
   RETAINED_SPELL * rSpell;
   rSpell = new_retained_spell();
   
   rSpell->name = icapitalize(name);
   rSpell->properties = spellProperties;
   rSpell->spellprint = spellPrintID;
   
   rSpell->next = ch->retain_first;
   ch->retain_first = rSpell;
   //rs->next = rSpell;
   ch->printlnf("'%s' has been added.", ch->retain_first->name);

}

Structure:
Code:
struct retained_spell
{
   RETAINED_SPELL *next
   bool      valid;
   char *    name;
   int      spellprint;
   long      properties;
};


And the really bad calamity, the new and free function. I know these must be fouled up, hell something I twisted with free caused it to become nuclear so that's why you see a lack of it being used, but I should have it for the do_unprepare function not shown.
Code:
RETAINED_SPELL      *   retain_free;
RETAINED_SPELL *new_retained_spell(void)
{
    RETAINED_SPELL *rs;
   
   if( !retain_free )
   {
      rs = (RETAINED_SPELL *)alloc_perm(sizeof(*rs));
   }
    else
    {
      rs = retain_free;
      retain_free = retain_free->next;
    }
   
   rs->name      =&str_empty[0];
   rs->spellprint = 0;
   rs->properties = 0;
   rs->next = NULL;
   
    return rs;
}
void free_retained_spell( RETAINED_SPELL *rs )
{
   /*
   if (!rs)
      return;*/
      
    if (!IS_VALID(rs))
      return;

   free_string( rs->name );
   rs->spellprint = 0;
   rs->properties = 0;
   
   INVALIDATE(rs);
   
   rs->next = retain_free;
   retain_free = rs;   
   return;
}


Back to top
View user's profile Send private message
Daos



Joined: 29 Jan 2003
Posts: 1219
Location: United States

PostPosted: Sat Jan 30, 2016 11:12 am    Post subject: Reply with quote

This sounds like a similar problem I had when I unintentionally was overloading printlnf(); when redesigning the 'bonus' command interface. Let me ponder over this and see what solutions I can conjure up for you.



_________________
- Daos

http://www.dawnoftime.org
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Trell



Joined: 28 Jun 2013
Posts: 49

PostPosted: Sun Feb 14, 2016 6:19 pm    Post subject: Hrmm Reply with quote

Well, right offhand I see two issues. The first is that in the function new_retained_spell() you never actually call the VALIDATE macro, so when you call free_retained_spell() it stops at the top and exits because of the !IS_VALID check so it's never free'd. The second issue is sort of related, but I am assuming it's just handled elsewhere. I didn't see anything actually freeing the retained spell, so it's hard to say if the linked list handling is correct in that aspect of it, but as far as adding to the linked list the code is solid, though you should actually use replace_string() to set rSpell->name instead of just setting it = to something.


Back to top
View user's profile Send private message
Oreant



Joined: 16 Jul 2010
Posts: 47

PostPosted: Mon Feb 15, 2016 6:38 am    Post subject: Reply with quote

The validate is a casualty of me eventually just ripping things around trying to quantify the error.

And free retained spell went bad for me. There was another function for removing items from the retained list, but it's in a pretty horrid mess. That part went from working to crashing.

Not using replace_string() was poor coding and sloppiness on my part.
And... I've put validate back in and using the replace_string()... and it seems to be working. I no longer am having name overwrites.

I need to clean up this bastardization and then repair my 'fixes' for the unprepare function. Should be able to just revert that back to a previous version and go from there.


Thanks so much for the help. I'm stupidly giggling way too much.
Good to have reminders of how much I don't understand anymore, and how much I probably never truly understood but sure thought I did Wink


Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    The Dawn Of Time Forum Index » Coding All times are GMT + 13 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001-2005 phpBB Group
Theme created by Vjacheslav Trushkin