Y
ypjofficial
Hello all,
I have encountered with following strange problem.
I am coding in C++ and using VC++ 6 compiler.
I have a class strvector containing char * cstr as a private member
and i have defined its destructor for releasing memory hold by cstr.
In main i created a vector<strvector>
and whenever i put a object of strvector inside this vector the program
crashes.
The complete program is as follows
/////////////////////////////////////////
#include <iostream.h>
#include "string"
#include "vector"
using namespace std;
class strvector
{
char * cstr;
public:
char * getcstr()
{
return cstr;
}
strvector(char * s)
{
cstr = new char[strlen(s)+1];
strcpy(cstr,s);
}
~strvector()
{
delete str;
}
/*release()
{
delete cstr;
}*/
};
void main()
{
vector<strvector> vec;
strvector str("one");
vec.push_back(str);//The program will crash
//str.release();
//cout<<vec[0].getcstr();//will print a garbage values.
}
///////////////////////////////////
If i remove the destructor and define a separate member function
release for releasing the memory,the cout statement will print the
garbase value.
Will someone please tell me what is going wrong in the above program
even if i am following a good programmering approach.(using destructor
etc.)?
Regards,
Yogesh Joshi
I have encountered with following strange problem.
I am coding in C++ and using VC++ 6 compiler.
I have a class strvector containing char * cstr as a private member
and i have defined its destructor for releasing memory hold by cstr.
In main i created a vector<strvector>
and whenever i put a object of strvector inside this vector the program
crashes.
The complete program is as follows
/////////////////////////////////////////
#include <iostream.h>
#include "string"
#include "vector"
using namespace std;
class strvector
{
char * cstr;
public:
char * getcstr()
{
return cstr;
}
strvector(char * s)
{
cstr = new char[strlen(s)+1];
strcpy(cstr,s);
}
~strvector()
{
delete str;
}
/*release()
{
delete cstr;
}*/
};
void main()
{
vector<strvector> vec;
strvector str("one");
vec.push_back(str);//The program will crash
//str.release();
//cout<<vec[0].getcstr();//will print a garbage values.
}
///////////////////////////////////
If i remove the destructor and define a separate member function
release for releasing the memory,the cout statement will print the
garbase value.
Will someone please tell me what is going wrong in the above program
even if i am following a good programmering approach.(using destructor
etc.)?
Regards,
Yogesh Joshi