assigning NULL to a struct pointer gives error and meaning of segmentation fault -
i have simple code of c,c++, runs fine:
struct node{ int x; struct node* next; }; int main(int argc, char *argv[]) { int a,b,c; struct node *root, *node1, *node2, *leaf; root = malloc( sizeof(struct node) ); //root = null; root->next = 0; root->x = 12; system("pause"); return 0; }
now if assign root = null or 0, instead of malloc, gives me error-
"an access violation (segmentation fault) raised in program" while in next line assign 0 next pointer. please explainin forum read segmentation fault occurs when try access secured memory space, while operating system defines page fault when required data not found in memory. related ? (sf of c, c++ , sf of operating system)
if set root = null;
, that, , not generate segmentation fault.
the segfault happens on next line, when try dereference null pointer.
if comment out 2 lines below it, observe no segfault.
Comments
Post a Comment