title: Why I am getting a Heap Corruption Error?
body: <p>I am new to C++. I am getting HEAP CORRUPTION ERROR. Any help will be highly appreciated. Below is my code  </p>
  
  <pre><code>class CEntity  
  {  
  //some member variables  
  CEntity(string section1,string section2);  
  CEntity();  
  virtual ~CEntity();  
  //pure virtual function ..  
  virtual CEntity* create()const = 0;  
  };  
  </code></pre>
  
  <p>I derive CLine from CEntity as below  </p>
  
  <pre><code>class CLine:public CEntity  
  {  
  // Again some variables ...  
  // Constructor and destructor  
  CLine(string section1,string section2);  
  CLine();  
  ~CLine();  
  CLine* Create() const;  
  }  
  
  // CLine Implementation  
  CLine::CLine(string section1,string section2) : CEntity(section1,section2){};  
  CLine::CLine();  
  CLine* CLine::create() const {return new CLine();}  
  </code></pre>
  
  <p>I have another class CReader which uses CLine object and populates it in a multimap  as below  </p>
  
  <pre><code>class CReader  
  {  
  public:  
  CReader();  
  ~CReader();  
  multimap&lt;int,CEntity*&gt;m_data_vs_entity;  
  };  
  
  //CReader Implementation  
  CReader::CReader()  
  {  
    m_data_vs_entity.clear();  
  };  
  
  CReader::~CReader()  
  {  
    multimap&lt;int,CEntity*&gt;::iterator iter;  
    for(iter = m_data_vs_entity.begin();iter!=m_data_vs_entity.end();iter++)  
    {  
      CEntity* current_entity = iter-&gt;second;  
      if(current_entity)  
        delete current_entity;  
    }  
    m_data_vs_entity.clear();  
  }  
  </code></pre>
  
  <p>I am reading the data from a file and then populating the CLine Class.The map gets populated in a function of CReader class. Since CEntity has a virtual destructor, I hope the piece of code in CReader's destructor should work. In fact, it does work for small files but I get HEAP CORRUPTION ERROR while working with bigger files. If there is something fundamentally wrong, then, please help me find it, as I have been scratching my head for quit some time now.<br>
  Thanks in advance and awaiting reply,<br>
  Regards,<br>
  Atul  </p>
  
  <p>Continued from Y'day : 
  Further studying this in detail I now have realized that Heap allocation error in my case is because I am allocating something, and then overwriting it with a higher size.
  Below is the code where in my data gets populated in the constructor.</p>
  
  <pre><code>CEntity::CEntity(string section1,string section2)
  {
      size_t length;
      char buffer[9];
      //Entity Type Number
      length = section1.copy(buffer,8,0);
      buffer[length]='\0';
      m_entity_type = atoi(buffer);
  
      //Parameter Data Count
      length = section1.copy(buffer,8,8);
      buffer[length]='\0';
      m_param_data_pointer = atoi(buffer);
          //.... like wise ....
  }
  </code></pre>
  
  <p>I am getting the values at a fixed interval of 8 chars and I am adding a '\0' so this, i guess will take care of any garbage value that I encounter.  </p>
  
  <p>About 
  <strong>Heap allocation error: after normal block (XXX) at XXX, CRT detected that application wrote to memory after end of Heap buffer.</strong> Mostly, Heap allocation errors occur somewhere else than where it crashes. I would appreciate if some one here would help me, how to make use of this normal block and the address.
  Thanks,</p>