Thread: struct question

  1. #46
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I am also assuming this has an error, as if I try to call this it is what creates the seg fault.. can you please tell me why?

    Code:
    rule_t* RuleConstruct(rule_t* rule, rule_t* newrule)
    {
      char** dep = (char**) malloc(sizeof(char*) * rule->depSize);
      int h;
      for (h = 0; h < rule->depSize; h++){
         dep[h] = malloc((strlen(rule->dependency[h]) + 1) * sizeof(char));
         strcpy(dep[h], rule->dependency[h]);
      } 
    
      char** bld = (char**) malloc(sizeof(char*) * rule->bldSize);
      int i;
      for (i = 0; i < rule->bldSize; i++){
         dep[i] = malloc((strlen(rule->buildcmd[i]) + 1) * sizeof(char));
         strcpy(dep[i], rule->buildcmd[i]);
      }
    
      char* target = (char*) malloc((strlen(rule->target) + 1) * sizeof(char));
      strcpy(target, rule->target);
       
      newrule->dependency = dep;
      newrule->buildcmd = bld;
      newrule->bldSize = rule->bldSize;
      newrule->depSize = rule->depSize;
      newrule->target = target;
    
      return newrule;
    }

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you tried reading the "debug guide" on cpwiki?

    http://cpwiki.sourceforge.net/Debugging

    A segfault should be easy to detect if you have a debugger at all on the machine (I'm guessing this is Linux, so try doing "gdb myprog", then "run", do the steps to make it crash, and "stack" to see where you ended up).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #48
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I am planning to use valgrind, but I don't know how so that valgrind can show me the line number in the error

  4. #49
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Probably because you call it from here:
    Code:
    for (temp = this->table[hashval]; temp != NULL; temp = temp->next){
        if (strcmp(key, temp->rule->target) == 0){
           RuleDestruct(rule);
           rule_t* newrule;
           temp->rule = RuleConstruct(rule, newrule);       
           return false;
        }
    And in this code rule is gone, since it was Destructed; but calling Construct without a valid rule in the first parameter leads to bad things.

    You'll have to decide what you want to do when you have two rules for the same target, but this ain't it.

  5. #50
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    even when calling the RuleDestruct alone it gives me the seg fault... so I guess I'll have to construct first and then destruct

    I should call RuleDestruct(temp->value) instead of RuleDestruct(rule).. fixed that now..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM