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; } 
  1. 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 explain

  2. in 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

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -