Thread: Where i'm wrong ? getting a segmentation fault

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    46

    Where i'm wrong ? getting a segmentation fault

    hi guys; I've tried to write a code whose name factors and this function takes a positive int as an argument and returns a binary tree which consists of the factors of the given int and Left child of any node in the tree should be the smallest int that is greater than 1, not equal to the node and divides the node without remainder. for example :
    210
    / \
    2 105
    / \
    3 35
    / \
    5 7
    but when ı run my code ı get seg. fault and where ı am wrong ı dont know is there anyone who can help me ?
    here is my code :
    Code:
    struct node
    {
            int value;
            struct node *left, *right;
            int depth;
    };
    
    
    typedef struct node node;
    
    
    node* factors(int num){
    int i=2;
    node * np ;
    node ** bas;
    bas=(node **)malloc(sizeof(node *));
    np=(node *)malloc(sizeof(node));
    bas=&np;
    np->value=num;
    
    
    while(i<=(num/2)){
    if(num%i==0){
    (np->left)->value=i;
    (np->right)->value=num/i;
    np=(np->right);
    }
    i+=1;
    }
    print_tree(np);
    return np;
    }

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    3 and 35 are under the 105 and 5 and 7 should be under the 35

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Boy... you paint a picture of a tree with 7 nodes but you have code that only seems to ever allocate memory for a single node. You need to have an allocation (malloc) for every node in the tree. You are also accessing pointers that are uninitialized. When you allocate memory for your first node and stick the value 210 in it, the left and right pointers have random values. You then try to access those pointers as if they point to valid locations, they do not. I'm guessing your return value should be the root pointer, not np as that would be pointing to the last node of the tree and this would seem to not be of any worth.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-17-2011, 10:55 AM
  2. Can anyone show me what is wrong here? Segmentation fault
    By BigFish21 in forum C Programming
    Replies: 17
    Last Post: 11-22-2007, 01:23 AM
  3. What can cause a Segmentation fault?
    By Mikecore in forum C Programming
    Replies: 3
    Last Post: 01-04-2006, 12:42 AM
  4. segmentation fault
    By C-learning in forum C Programming
    Replies: 2
    Last Post: 12-16-2002, 11:48 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM