-
How to free memory ?
This is my code for operator overloading;
where should i free memory in class with distructor ? if i do that i am getting exception.
Code:
--------------------------------
in Test.h file
----------------------------------
class demon
{
char* pName;
int nVal,nLen;
public:
demon operator= (const demon& d)
{
if(this != &d)
{
this->pName = new char(strlen(d.pName)+1);
strcpy(pName,d.pName);
this->nVal = d.nVal;
}
return *this;
}
void set(int x,char* pstring)
{
nVal = x;
pName = (char*)malloc(strlen(pstring)+1);
strcpy(pName ,pstring);
}
void get()
{
cout<<nVal<<endl<<pName<<endl;
}
demon operator + (const demon& d1)//,const demon d2);
{
nLen =strlen(this->pName)+1;
nLen += strlen(d1.pName)+1;
this->pName = (char*)realloc(this->pName,nLen);
strcat(this->pName, d1.pName);
return *this;
}
};
------------------------------------
in test.cpp fiel
---------------------------------------
void main()
{
demon obj_demon, obj_demon2 ;
obj_demon.set(25,"mySample");
obj_demon2.set(45,"Overload demo"); // = obj_demon1 = obj_demon;
obj_demon = obj_demon + obj_demon2;
obj_demon.get();
obj_demon2.get();
return;
}
-
> where should i free memory in class with distructor ?
Yes, you should free memory in the destructor.
> if i do that i am getting exception.
That's because your code is full of bugs.
1. You don't have a proper constructor to begin with, which at least sets pName = NULL;
2. Mixing new and malloc/realloc is a bad thing to do. Since this is C++, you should be using new for everything.
> void main()
See the FAQ - main returns int.
-
You should really use std::string instead of char* in which case you wouldn't need practically any of this code.
-
thx salem,
Did operator "new" resembles to "realloc" functionality ?
say i initilization for pointer in constructor and free memory in destructor. still my code
gives run time error at destructor.
i have just switched from C to C++ programming.
Code:
---------------
test.h file
---------------------------
class demon
{
char* pName;
int nVal,nLen;
public:
demon():nVal(0),nLen(0)
{
pName = NULL;
}
demon operator= (const demon& d)
{
if(this != &d)
{
this->pName = new char(strlen(d.pName)+1);
memset(this->pName,0,strlen(d.pName)+1);
strcpy(pName,d.pName);
this->nVal = d.nVal;
}
return *this;
}
void set(int x,char* pstring)
{
nVal = x;
pName = (char*)malloc(strlen(pstring)+1);
memset(this->pName,0,strlen(pstring)+1);
strcpy(pName ,pstring);
}
void get()
{
cout<<nVal<<endl<<pName<<endl;
}
demon operator + (const demon& d1)//,const demon d2);
{
nLen =strlen(this->pName)+1;
nLen += strlen(d1.pName)+1;
this->pName = (char*)realloc(this->pName,nLen);
memset(this->pName,0,nLen);
strcat(this->pName, d1.pName);
return *this;
}
~demon()
{
delete(pName);
}
};
-----------------------
in test.cpp file
---------------------------
main()
{
demon obj_demon, obj_demon1, obj_demon2 ;
obj_demon.set(25,"mySample");
obj_demon2.set(45,"Overload demo");
obj_demon = obj_demon + obj_demon2;
obj_demon.get();
obj_demon2.get();
return 0;
}
-
To allocate a char array use square brackets:
Code:
pName = new char[how_many];
...
delete [] pName;
In addition you'll need a copy constructor. And the assignment operator leaks memory because you don't free the old memory before you request for new memory.
You shouldn't be using realloc with a pointer that you obtained with new (in C++ you'd just free previous memory and allocate a larger new block).
By the way, for a char* alloc, free and realloc would be fine if you want to stick with char arrays. If you want to write programs, though :), and not just sweat over the low-level char array manipulation, strings are preferable.
Here's what the same code would look like, using C++ string class.
Code:
#include <string>
class demon
{
string Name;
int nVal;
public:
demon():nVal(0) {}
void set(int x, string s)
{
nVal = x;
Name = s;
}
void get()
{
cout<<nVal<<endl<<Name<<endl;
}
demon operator + (const demon& d1)
{
Name += d1.Name;
return *this;
}
};
Note, that neither copy constructor, assignment operator nor destructor is needed, as the default ones that the compiler produces are good enough.
-
> Did operator "new" resembles to "realloc" functionality ?
There is no C++ operator which resembles realloc in C.
You have to call new[ ] for some new memory, followed by a manual copy in your code (use a for loop), and a delete[ ] of the old memory.
And you're still mixing new with malloc/realloc.
> demon():nVal(0),nLen(0)
Why not
demon():nVal(0),nLen(0),pName(0)
> main()
It's better than void, but why not just be specific and say
int main()
-
> Did operator "new" resembles to "realloc" functionality ?
If you use C++ vectors instead of dynamic arrays (generally a good idea), you can use what's known as the "swap trick" to do essentially what realloc() does with dynamic arrays. For example, see
http://www.informit.com/guides/conte...eqNum=268&rl=1
-
Thanks to all for your valuable suggestions.
I will try to skick more to c++ manual.
-
operator + should not modify either of the operands. Declare the function as const and correct the behaviour, or better yet, use the two-parameter friend version with both parameters as const.