Heap corruption error...........How to solve it?

#include<iostream>
#include<conio.h>
using namespace std;
class Person{
protected:
char *name,*nationality,*dateofbirth;
public:
Person()
{
name=new char[3];
nationality=new char[3];
dateofbirth=new char[3];
cout<<"Enter the name :";
cin>>name;
cout<<"Nationality :";
cin>>nationality;
cout<<"Date of birth :";
cin>>dateofbirth;}
void print(){
cout<<"Name :"<<name<<endl;
cout<<"Nationality :"<<nationality<<endl;
cout<<"Date of Birth :"<<dateofbirth<<endl;
}
~Person(){
delete[] name;
delete[] dateofbirth;
delete[] nationality;}
};
class student:public Person{
char *dateofgraduation,*gpa;
public:
student(){
dateofgraduation=new char[3];
gpa=new char[3];
cout<<"Enter date of graduation :";
cin>>dateofgraduation;
cout<<"Enter GPA :";
cin>>gpa;}
void display(){
cout<<"Date of GRaduation :"<<dateofgraduation<<endl;
cout<<"GPA :"<<gpa<<endl;}
~student(){
delete[] dateofgraduation;
delete[] gpa;}
};
void main(){
student s;
s.print();
s.display();
getch();}
closed account (zb0S216C)
The common cause of heap corruptions is the deletion of memory that was already deleted. I'll exemplify this scenario here:

1
2
3
4
int *p_memory(new int(10));

delete p_memory; // OK.
delete p_memory; // Not OK. 'p_memory' was already deleted. 

Another scenario is when another pointer points to a region of memory allocated by some other pointer and then deletes it. For instance:

1
2
3
4
5
int *p_memory(new int(10));
int *p_handle(p_memory);

delete p_handle;  // 'p_memory's memory has been deleted.
delete p_memory;  // 'p_memory' was already deleted. 

Does your code contain any of these scenarios?

Wazzak
Last edited on
you're allocating only 3 char for name and the other data. That means if the user enters 3+ chars it's likely that it crashes because data is written beyond the end.
The error is due to the propogation of heap memory if the user enter more than 3 characters ...............then how can I solve this problem????????
BUT in a case also wher user enter only 3 characters same as the size of memory defined...........the error is same...................
If you wrote

1
2
3
name=new char[3];
nationality=new char[3];
dateofbirth=new char[3];

You have to limit your input

1
2
3
cin >> setw(3) >> name;
cin >> setw(3) >> nationality;
cin >> setw(3) >> dateofbirth;

But I am not sure what kind of names and nationalities are 2-letter long (3 character array stores 2 letters and terminating null). Why not use strings?
But if I use
name=new char;
nationality=new char;
dateofbirth=new char;

then how to deallocate the memory in heap.......when I use
delete name;
delete nationality;
delete dateofbirth;
it gives me heap corruption error.............
Could you please print here an error that you are looking at? (Copy Past from IDE, compiler, terminal, whatever).
Ahmad Haseeb Rabbani wrote:
BUT in a case also wher user enter only 3 characters same as the size of memory defined...........the error is same...................
Yes, entering 3 is too much since there will be automatially a '\0' add and that makes 4 -> out of bounds -> crash.

But why so sparse? why not 30 name=new char[30]; or 60...
Or std::string...
Why this error occurs in Visual Studio but not in DEV C++................?
Undefined behaviour, anything can happen.
HI ,
if the array are defined as name= new char[30] and delete the char pointer as delete [] name; you should not get the problem .
Topic archived. No new replies allowed.