Returning a null pointer from a C function, comparison seg faults -
i'm working on file system simulator using c. node struct looks this:
typedef struct node { char name[64]; char type; struct node* child; struct node* sibling; struct node* parent; }node;
my recursive function, findnode(), returns null if can't find node in tree specific name.
node* findnode(char* name, node* start) { printf("inside findnode, looking %s, starting @ %s\n", name, start->name); node* current = start; if(strcmp(name, current->name) == 0) { printf("inside findnoe, node found.\n"); return current; } if(current->sibling->name != 0) { return findnode(name, current->sibling); } if(current->child->name != 0) { return findnode(name, current->child); } printf("inside findnode, node not found.\n"); return 0; }
upon calling findnode() , comparing 0, program seg faults.
if(findnode(bname,current) != 0) //seg fault here { printf("error: node basename exists under directory name.\n"); return; }
i know program reaching end of findnode() when hasn't found node, because prints "node not found" message.
i'm under impression it's possible return 0 when return type pointer. i've tried using null, , saving result node* variable before comparing it. neither worked.
can explain what's going on here? i'd appreciate it. in advance.
edit: code looks following. there's new check @ beginning see if current
null, , i've stopped trying access child
, sibling
names.
node* findnode(char* name, node* start) { node* current = start; if(current == null) { printf("inside findnode, null passed in\n"); return null; } printf("inside findnode, looking %s, starting @ %s\n", name, current->name); if(strcmp(name, current->name) == 0) { printf("inside findnode, node found.\n"); return current; } if(current->sibling != null && current->sibling != root) { return findnode(name, current->sibling); } if(current->child != null && current->sibling != root) { return findnode(name, current->child); } printf("inside findnode, node not found.\n"); return null; }
first test: "/" , root node, name "/". second test: "hello" , root node, name "/". "hello" should not found.
inside findnode, looking /, starting @ / inside findnode, node found. inside findnode, looking hello, starting @ / inside findnode, node not found. segmentation fault
i've wrote program test code. notice in main function initialized null pointer.
#include <iostream> using namespace std; typedef struct node { char name[64]; char type; struct node* child; struct node* sibling; struct node* parent; }node; node* findnode(char* name, node* start) { printf("inside findnode, looking %s, starting @ %s\n", name, start->name); node* current = start; if (strcmp(name, current->name) == 0) { printf("inside findnoe, node found.\n"); return current; } if (current->sibling->name != 0) { return findnode(name, current->sibling); } if (current->child->name != 0) { return findnode(name, current->child); } printf("inside findnode, node not found.\n"); return 0; } int main() { node *n = null; char bname[] = "somename"; node *current = n; if (findnode(bname, current) != 0) //seg fault here { printf("error: node basename exists under directory name.\n"); } }
the output is(used visual studio 2015)
inside findnode, looking somename, starting @ (null)
so kaylum
pointed out, line printf("inside findnode, looking %s, starting @ %s\n", name, start->name);
causes segmentation fault. , reason because trying access pointer null.
at first need check, if pointer accessing null or not. must access attributes or values.
i hope makes sense.
Comments
Post a Comment