Home > Net >  Segmentation fault (core dumped) - Use of uninitialised value of size 8
Segmentation fault (core dumped) - Use of uninitialised value of size 8

Time:11-03

  #include <iostream>
  #include <string>
  #include <list>
  #include <algorithm>
  using namespace std;
  class Node{ 
      private:
          Node *parent;
          string name;
      public:
          Node(){
          }
          Node(string nodeName, Node *nodeParent){
              setName(nodeName);
              setParent(nodeParent);
          }
          Node getParent(){
              return *parent;
          }
          void setParent(Node *p){
             parent = p;
          }
          string getName(){
              return name;
          }   
          void setName(string n){
              name = n;    //<--this is where the code fails...
          }
  };
  class Child1 : public Node {             
  };
  class Child2 : public Node {     
  };
  class Graph {
      private:
          string name;
          Node root;
          void setName(string n){  name = n;   }
          void setRoot(Node r){  root = r;   }   
      public:
          Graph(string n, Node r){ //Constructor
              setName(n);
              setRoot(r);
              Node *root = new Node();
              string name = "Root";
              root->setName(name);
  
              Node *node1 = new Node("Node1", root);
              Node *node2 = new Node("Node2", root);
              Node *node3 = new Node("Node3", node2);
          
              Child1 *child1;  
              child1->setName("Child1");
              child1->setParent(root);
  
              Child2 *child2;
              child2->setName("Child2");
              child2->setParent(node1);    
          }
          string getName(){
              return name;
          }
          Node getRoot(){           
              return root;
          }
  };
  int main() {
      Node *root = new Node();
      root->setName("Root");
      Graph *sg = new Graph("Graph", *root);
  };

Hello everyone. I am trying to build a node structure. But, as you can see, I am not very good at c . I am getting Segmentation fault (core dumped) error in Visual Studio Code. So also tried in https://pythontutor.com/ and it gave me ERROR: Use of uninitialised value of size 8. I could not find where to initialize name. Can you help me?

Also the code is not efficient. I had to define two separate root: one in main() and one in Graph Constructor because could not figure out how to use pointers. If you can explain a way to make it more efficient, it would be great. But it is not that necessary right now.

EDIT

I created constructures for Child1 and Child2. Then change: Child1 *child1; to Child1 *child1 = new Child1(); Child2 *child2; to Child2 *child2 = new Child2(); The error is gone.

CodePudding user response:

Child1 *child1;  
child1->setName("Child1");
child1->setParent(root);
  
Child2 *child2;
child2->setName("Child2");
child2->setParent(node1);

With both of these, you're taking an uninitialized pointer (child1 and child2) and trying to dereference it.

You probably want something on this order:

Child1 *child1 = new Child1;
child1->setName("Child1");
// ...

Child2 *child2 = new Child2;
// ...

It's going to take a bit more than that to actually create a graph, but this will at least fix the error message you're seeing. To do something very meaningful, you're going to want each node to container at least one (and probably an arbitrary number of) pointers to child nodes. Then you'll want the root's child pointer to point to child1, and child1's child pointer to point to child2 (or something on that order).

  • Related