Run time error in mlock.c

I have one Node parent class in Node.h and two children that inherit from Node, Sense and Word in Sense.h and Word.h.



In my c++ code file, I call Word and Sense. But word works fine, wile Sense throws a memory run time error. This is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//this code works well
Word node_word;
node_word.SetNodeLabel(node_string);
node_word.SetNodeID(1);

//this code has a bad pointer to the label, and I use the same approach for 
//Sense and Word. But Word works fine...

Sense word_sense;
//this is where code fails with a run time error in mlock.c and word_sense 
//has a bad pointer to label in node

word_sense.SetNodeLabel(sense_str);
word_sense.SetExampleUseString(eg_use);
word_sense.SetNoTagged(GetTagcnt(idx, sense + 1));


What am I doing wrong?
Last edited on
I have not looked at the code in detail, but at a quick glance I can see several major errors that stem from not
understanding how to construct the base class portion of a derived class.

This does nothing useful:

1
2
3
4
5
	Sense(std::string l,int id,std::string egU, int no)
	{
                // ... snip ...
		Node::Node(l,id);   // <- constructs a temporary; does not construct the base
	}


Correct:
1
2
3
4
5
	Sense(std::string l,int id,std::string egU, int no) : Node( l, id )
	{
       		exampleUse="";
		no_tagged=0; 
	}


And even better:

1
2
3
	Sense(std::string l,int id,std::string egU, int no) : Node( l, id ), exampleUse(), no_tagged()
	{
	}


Read up on initializer lists.
Topic archived. No new replies allowed.