V
Vishu
Hi ,
I am having a problem with a very naieve looking code. This code runs
well on Sun Sparc, 5.9(compiled with g++); but faults at the line
"delete [] name" on Windows (I tried both VC 6.0 and .net 2003
compilers). The code is so basic, I dont suspect any platform specific
issue; which is why I am posting it here.. Can anyone shed some light??
///////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace::std;
using std::cout;
using std::endl;
class abcd
{
public:
abcd(char *);
~abcd();
void print();
private:
char *name;
};
abcd::abcd(char * nameVal):name(NULL)
{
if(nameVal)
{
name = new char[strlen(nameVal)];
if(name)
{
memset(name,0,strlen(nameVal));//no need of
this..but still..
// memcpy(name,nameVal,strlen(nameVal));
strcpy(name,nameVal);
}
}
}
void abcd:rint()
{
if(name)
{
cout<<"Value is " << name <<endl;
}
}
abcd::~abcd()
{
if(name)
{
// free(name);
delete [] name;
cout << "Deleted successfully" <<endl;
name = NULL;
}
}
int main()
{
abcd * dude = new abcd("whatthehell");
dude->print();
delete dude;dude = NULL;
return 0;
}
///////////////////////////////////////////////////////////////////////
I am having a problem with a very naieve looking code. This code runs
well on Sun Sparc, 5.9(compiled with g++); but faults at the line
"delete [] name" on Windows (I tried both VC 6.0 and .net 2003
compilers). The code is so basic, I dont suspect any platform specific
issue; which is why I am posting it here.. Can anyone shed some light??
///////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace::std;
using std::cout;
using std::endl;
class abcd
{
public:
abcd(char *);
~abcd();
void print();
private:
char *name;
};
abcd::abcd(char * nameVal):name(NULL)
{
if(nameVal)
{
name = new char[strlen(nameVal)];
if(name)
{
memset(name,0,strlen(nameVal));//no need of
this..but still..
// memcpy(name,nameVal,strlen(nameVal));
strcpy(name,nameVal);
}
}
}
void abcd:rint()
{
if(name)
{
cout<<"Value is " << name <<endl;
}
}
abcd::~abcd()
{
if(name)
{
// free(name);
delete [] name;
cout << "Deleted successfully" <<endl;
name = NULL;
}
}
int main()
{
abcd * dude = new abcd("whatthehell");
dude->print();
delete dude;dude = NULL;
return 0;
}
///////////////////////////////////////////////////////////////////////