Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 684449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:49:05+00:00 2026-05-14T01:49:05+00:00

I am new to C++. I am getting HEAP CORRUPTION ERROR. Any help will

  • 0

I am new to C++. I am getting HEAP CORRUPTION ERROR. Any help will be highly appreciated. Below is my code

class CEntity  
{  
//some member variables  
CEntity(string section1,string section2);  
CEntity();  
virtual ~CEntity();  
//pure virtual function ..  
virtual CEntity* create()const = 0;  
};  

I derive CLine from CEntity as below

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();}  

I have another class CReader which uses CLine object and populates it in a multimap as below

class CReader  
{  
public:  
CReader();  
~CReader();  
multimap<int,CEntity*>m_data_vs_entity;  
};  

//CReader Implementation  
CReader::CReader()  
{  
  m_data_vs_entity.clear();  
};  

CReader::~CReader()  
{  
  multimap<int,CEntity*>::iterator iter;  
  for(iter = m_data_vs_entity.begin();iter!=m_data_vs_entity.end();iter++)  
  {  
    CEntity* current_entity = iter->second;  
    if(current_entity)  
      delete current_entity;  
  }  
  m_data_vs_entity.clear();  
}  

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.
Thanks in advance and awaiting reply,
Regards,
Atul

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.

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 ....
}

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.

About
Heap allocation error: after normal block (XXX) at XXX, CRT detected that application wrote to memory after end of Heap buffer. 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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:49:06+00:00Added an answer on May 14, 2026 at 1:49 am

    Finally, after two days of debugging, I was able to fix up the crash. It was due to me copying wrong number of characters from the string.
    Lessons learnt :
    1. When you encounter memory allocation errors, try to form a simple test case which has minimum entities to reproduce the problem.
    2. A sure shot way is a line by line debugging. I agree,it test your patience, but then, there are no short cuts to success
    3. And it gives you a chance to do a code review, further enhancing the quality of code that you produce in future

    Thank you for all your help and formatting my code 🙂
    Regards,
    Atul

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.