Memory Read Error while accessing pointers in object
<p>I have subdivided my question into different sections for better understanding. They are as below:</p>

<h2>A) Classes My Question is related to</h2>

<p>I have a class TLst representing list as below:</p>

<pre><code>template &lt;class TVal&gt;
class TLst{
public:
  typedef TLstNd&lt;TVal&gt;* PLstNd;
private:
  int Nds;
  PLstNd FirstNd;
  PLstNd LastNd;
public:
  TLst(): Nds(0), FirstNd(NULL), LastNd(NULL){}
  TLst(const TLst&amp;);
  ~TLst(){Clr();}
  explicit TLst(TSIn&amp; SIn);
  void Save(TSOut&amp; SOut) const;

**TLst &lt;TVal&gt;&amp; operator = (const TLst &lt;TVal&gt; &amp;t)
  {
    TLst&lt;TVal&gt;::PLstNd Nd;
    if (Len()!=t.Len()) { Clr(); }
    for (TLst&lt;TVal&gt;::PLstNd np = t.First(); np!=NULL ; np=np-&gt;Next()) {
        Nd = AddBack(np-&gt;GetVal()); 
    }
    return *this;
  }**

  void Clr(){
    PLstNd Nd=FirstNd;
    while (Nd!=NULL){PLstNd NextNd=Nd-&gt;NextNd; delete Nd; Nd=NextNd;}
    Nds=0; FirstNd=NULL; LastNd=NULL;}

  bool Empty() const {return Nds==0;}
  int Len() const {return Nds;}
  PLstNd First() const {return FirstNd;}
  PLstNd Last() const {return LastNd;}

**PLstNd AddBack(const TVal&amp; Val){
    PLstNd Nd=new TLstNd&lt;TVal&gt;(LastNd, NULL, Val);
    if (LastNd!=NULL){LastNd-&gt;NextNd=Nd; LastNd=Nd;}
    else {FirstNd=Nd; LastNd=Nd;}
    Nds++; return Nd;
  }**

  PLstNd AddFrontSorted(const TVal&amp; Val, const bool&amp; Asc=true);
  PLstNd AddBackSorted(const TVal&amp; Val, const bool&amp; Asc=true);
  void PutFront(const PLstNd&amp; Nd);
  void PutBack(const PLstNd&amp; Nd);
  PLstNd Ins(const PLstNd&amp; Nd, const TVal&amp; Val);
  void Del(const TVal&amp; Val);
  void Del(const PLstNd&amp; Nd);

  PLstNd SearchForw(const TVal&amp; Val);
  PLstNd SearchBack(const TVal&amp; Val);

  friend class TLstNd&lt;TVal&gt;;
};
</code></pre>

<p>Similarly I have the class TLstNd to represent a list-node as below:</p>

<pre><code>template &lt;class TVal&gt;
class TLstNd{
public:
  TLstNd* PrevNd;
  TLstNd* NextNd;
  TVal Val;
public:
  TLstNd(): PrevNd(NULL), NextNd(NULL), Val(){}
  TLstNd(const TLstNd&amp;);
  **TLstNd(TLstNd* _PrevNd, TLstNd* _NextNd, const TVal&amp; _Val):
    PrevNd(_PrevNd), NextNd(_NextNd), Val(_Val){}**

  TLstNd&amp; operator=(const TLstNd&amp;);

  TLstNd* Prev() const {Assert(this!=NULL); return PrevNd;}
  TLstNd* Next() const {Assert(this!=NULL); return NextNd;}
  TVal&amp; GetVal(){Assert(this!=NULL); return Val;}
};
</code></pre>

<p>And I have the members and constructors of class TVec (not the entire class) as shown below</p>

<pre><code>template &lt;class TVal&gt;
class TVec{
public:
  typedef TVal* TIter;
protected:
  int MxVals; // if MxVals==-1, then ValT is not owned by us, we don't free it!
  int Vals;
  TVal* ValT;
  void Resize(const int&amp; _MxVals=-1);
  TStr GetXOutOfBoundsErrMsg(const int&amp; ValN) const;
public:
  TVec(): MxVals(0), Vals(0), ValT(NULL){}
  TVec(const TVec&amp; Vec);
  **explicit TVec(const int&amp; _Vals){
    IAssert(0&lt;=_Vals); MxVals=Vals=_Vals;
    if (_Vals==0){ValT=NULL;} else {ValT=new TVal[_Vals];}}**
  TVec(const int&amp; _MxVals, const int&amp; _Vals){
    IAssert((0&lt;=_Vals)&amp;&amp;(_Vals&lt;=_MxVals)); MxVals=_MxVals; Vals=_Vals;
    if (_MxVals==0){ValT=NULL;} else {ValT=new TVal[_MxVals];}}
  explicit TVec(TVal *_ValT, const int&amp; _Vals):
    MxVals(-1), Vals(_Vals), ValT(_ValT){}

...

};
</code></pre>

<p>And I have the class TPair as below:</p>

<pre><code>class TPair{
public:
  TVal1 Val1;
  TVal2 Val2;
public:
  TPair(): Val1(), Val2(){}
  TPair(const TPair&amp; Pair): Val1(Pair.Val1), Val2(Pair.Val2){}
  **TPair(const TVal1&amp; _Val1, const TVal2&amp; _Val2): Val1(_Val1), Val2(_Val2){}**
...
  TPair&amp; operator=(const TPair&amp; Pair){
    if (this!=&amp;Pair){Val1=Pair.Val1; Val2=Pair.Val2;} return *this;}
  bool operator==(const TPair&amp; Pair) const {
    return (Val1==Pair.Val1)&amp;&amp;(Val2==Pair.Val2);}
...
};
</code></pre>

<h2>B) What I am doing?</h2>

<p>a) I have a TVec>> class "typedef"-ed as</p>

<pre><code>typedef TVec&lt;TPair&lt;TInt, TLst&lt;TInt&gt;&gt;&gt; TNdClass;
</code></pre>

<p>b) Next, I create an object of TNdClass, as</p>

<pre><code>TNdClass UsrClassList(MXCLASS_SIZE); //calls TVec(const int&amp; _Vals)
</code></pre>

<p>If you see the TVec constructor which is bold, the above call sets Vals, MxVals and allocates MxVals memory for ValT which means UsrClassList allocates memory of size MXCLASS_SIZE*sizeof(TPair>). </p>

<p>c) Next, I am assigning Val1 (of type TInt) and Val2 (of type TLst) to two variables NdCnt (of type TInt) and Nodes (of type TLst). And declare a variable NdClss of type TPair> as below:</p>

<pre><code>TInt NdCnt(0);
TLst&lt;TInt&gt; Nodes;
NdCnt = UsrClassList[VecIdx].Val1; //assigning TInt to TInt
Nodes = UsrClassList[VecIdx].Val2; //assigning TLst&lt;TInt&gt; to TLst&lt;TInt&gt;
TPair&lt;TInt, TLst&lt;TInt&gt; &gt; NdClss; //declaration
</code></pre>

<p>d) Next I am adding a TInt of value 1 to Nodes using the AddBack function of TLst (see above in bold) and assign a TPair object instantiated with NdCnt and Nodes to NdClassas below:</p>

<pre><code>Nodes-&gt;AddBack(1); //adding a TInt of value 1 to Nodes
</code></pre>

<p>e) Finally I am creating a new object of type TPair with NdCnt and Nodes values as parameter and is assigning to NdClss variable (type TPair) declared in step c). </p>

<pre><code>NdClss = TPair&lt;TInt, TLst&lt;TInt&gt;&gt;(NdCnt, Nodes); 
</code></pre>

<p>This step ends up with an exception with memory read error.</p>

<p>Further down the code, when I am debugging the values of Nodes.First()->NextNd or Nodes.First()->PrevNd the compiler gives "memory read error" which I feel indicates that the NextNd and PrevNd pointers are not allocated with memory.</p>

<p>When I debug the code, I get the value '???' for the watch variables PrevNd and NextNd:</p>

<pre><code>-       FirstNd 0x00e6b830 {PrevNd=0x00000000 NextNd=0x00000000 Val={...} } TLstNd&lt;TInt&gt; *
+       PrevNd  0x00000000 {PrevNd=??? NextNd=??? Val={...} }   TLstNd&lt;TInt&gt; *
+       NextNd  0x00000000 {PrevNd=??? NextNd=??? Val={...} }   TLstNd&lt;TInt&gt; *
+       Val {Val=0 Mn=-2147483648 Mx=2147483647 ...}    TInt
-       LastNd  0x00e6b830 {PrevNd=0x00000000 NextNd=0x00000000 Val={...} } TLstNd&lt;TInt&gt; *
+       PrevNd  0x00000000 {PrevNd=??? NextNd=??? Val={...} }   TLstNd&lt;TInt&gt; *
+       NextNd  0x00000000 {PrevNd=??? NextNd=??? Val={...} }   TLstNd&lt;TInt&gt; *
+       Val {Val=0 Mn=-2147483648 Mx=2147483647 ...}    TInt
</code></pre>

<p>Any suggestions of why the memory for Nodes.First()->NextNd and Nodes.First()->PrevNd are not allocated? And what I need to do to allocate the memory of those two pointers in TLst class?</p>

<p>Look forward to replies.</p>

<p>Somnath</p>
