Thread: error handling in recursive function.

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    87

    error handling in recursive function.

    hello can some one please explain me how to do error handling in a recursive function. for eg. the following is a recursive function :

    Code:
    void subdivide_kdtree(kdnode *kd, mesh *m, int depth, int maxdepth, int maxtriangles, int axis)
    {
      int i, nextaxis;
      kd->child[0] = kd->child[1] = NULL;
      trinode *ptr, *last;
       
      if(kd->ntriangles > maxtriangles && depth < maxdepth)
      {
        for(i = 0; i < 2; i++)
        {
          kd->child[i] = malloc(sizeof(kdnode));
          kd->child[i]->box = kd->box;
          kd->child[i]->trifirst = NULL;
          kd->child[i]->ntriangles = 0;
          kd->splitplane = axis;
          kd->child[i]->box.minB.coord[axis] = kd->box.minB.coord[axis] + 0.5 * i * (kd->box.maxB.coord[axis] - kd->box.minB.coord[axis]);
          kd->child[i]->box.maxB.coord[axis] = kd->box.minB.coord[axis] + 0.5 * (i+1) * (kd->box.maxB.coord[axis] - kd->box.minB.coord[axis]);
          nextaxis = (axis + 1) &#37; 3;      
          ptr = kd->trifirst;
          while(ptr != NULL)
          {
            if(insidebox(kd->child[i], m, ptr->tindex, axis, i))
            {
              add_to_triangle_list(&kd->child[i]->trifirst, &last, ptr->tindex);
              kd->child[i]->ntriangles += 1;
            }
            ptr = ptr->next;
          }
          subdivide(kd->child[i], m, depth + 1, maxdepth, maxtriangles, nextaxis);
        }
      }
    }
    Notice the bolded part. If malloc fails I usually do error handlling by passing a flag which indicates an error has happened and i also print an error message. just for the sake of an example:

    Code:
    int some_function(void)
    {
       double *p;
        int flag = 0;
    
       p = malloc(sizeof(*p) * 500);
       if(p == NULL)
      {
        fprintf(stderr, "\n%s line %d : Memory allocation failure\n", __FILE__, __LINE__);
        flag = 1;
        return flag;
      }
    
      return flag;
    }
    I mean how to pass this flag in a recursive function is what i can't figure out. pls help me out.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since a recursive function typically keeps returning what it returns after calling itself, you can just return an error flag. Ie:

    malloc(...);
    if (error)
    return error_flag;
    return myfunction(...);

    The entire chain will then return your error flag.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by Elysia View Post
    Since a recursive function typically keeps returning what it returns after calling itself, you can just return an error flag. Ie:

    malloc(...);
    if (error)
    return error_flag;
    return myfunction(...);

    The entire chain will then return your error flag.
    can you please elaborate this statement:

    return myfunction() ;

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by broli86 View Post
    can you please elaborate this statement:

    return myfunction() ;
    That's your recursive call. Obviously you'd need to fill in the () bit with n/2 or n-1 or whatever.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by tabstop View Post
    That's your recursive call. Obviously you'd need to fill in the () bit with n/2 or n-1 or whatever.
    yeah basically the arguments of the recursive function.

    return myfunction(arg1, arg2, arg3);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM