K
kk
Hi all,
i didn't get output in the following code while compiling and executing
with g++ version 3.2.3 it doesn't allocate memory to pointer varaible
(x) in class B. and it gives correct output while executing in .Net and
visual slick editor. why it didn't allocate memory? if anybody knows
plz give reply.
thanks in advance
kk
------File Name: les9_5.C--------------
#include <iostream>
#include <string.h>
using namespace std;
class B{
char *x;
public:
B(char *y="xxx"):x(new char[strlen(y)+1]){
try{
// x=new char[strlen(y)+1];
if(*x)
strcpy(x,y);
else
{
cout<<"value at x\t"<<x<<endl;
cout<<y<<endl;//checkig formal
parameter
throw 1;
}
}catch(...){
cout<<"Constructor B(char *y=\"xxx\") "<<endl;
cout<<"memory allocation failure"<<endl;
}
}
B(B *objb):x(new char[strlen(objb->x)+1])
{
cout<<"constructor B(B *objb):x(new
char[strlen(objb->x)+1])"<<endl;
if(*x)
strcpy(x,objb->x);
else
cout<<"memory allocation failure"<<endl;
}
B(const B& objb)
{
x=new char[strlen(objb.x)+1];
if(*x)
strcpy(x,objb.x);
else
cout<<"memory allocation failure"<<endl;
}
void showx()
{
cout<<"value of x from the object of class
B="<<x<<endl; }
~B(){
try{
if(*x)
delete []x;
else throw 2;
}catch(...){
cout<<"memory already deleted"<<endl;
}
}
};
class A{
char *y;
B *b;
public:
A(char *c="xxx",char *d="bbb"):y(new char[strlen(c)+1]),b(new
B(d)){
try{
// y=new char[strlen(c)+1];
// b=new B(d);
if(*y||*d)
strcpy(y,c);
else
throw 2;
}catch(...){
cout<<"memory allocation failure";
}
}
void showy()
{
cout<<"value of y from the object of class
B="<<y<<endl;
b->showx();
}
~A(){
try{
if(*y)
delete []y;
else throw 2;
/* if(*b)
delete []b;
else throw 2;
*/
}catch(...){
cout<<"memory already deleted in A's
destructor"<<endl;
}
}
};
main()
{
A a("Hello","Gates");
a.showy();
}
compiling: g++ -g -o les9_5 les9_5.C
executing: ./les9_5
output:
value at x
Gates
Constructor B(char *y="xxx")
memory allocation failure
value of y from the object of class B=Hello
value of x from the object of class B=
i didn't get output in the following code while compiling and executing
with g++ version 3.2.3 it doesn't allocate memory to pointer varaible
(x) in class B. and it gives correct output while executing in .Net and
visual slick editor. why it didn't allocate memory? if anybody knows
plz give reply.
thanks in advance
kk
------File Name: les9_5.C--------------
#include <iostream>
#include <string.h>
using namespace std;
class B{
char *x;
public:
B(char *y="xxx"):x(new char[strlen(y)+1]){
try{
// x=new char[strlen(y)+1];
if(*x)
strcpy(x,y);
else
{
cout<<"value at x\t"<<x<<endl;
cout<<y<<endl;//checkig formal
parameter
throw 1;
}
}catch(...){
cout<<"Constructor B(char *y=\"xxx\") "<<endl;
cout<<"memory allocation failure"<<endl;
}
}
B(B *objb):x(new char[strlen(objb->x)+1])
{
cout<<"constructor B(B *objb):x(new
char[strlen(objb->x)+1])"<<endl;
if(*x)
strcpy(x,objb->x);
else
cout<<"memory allocation failure"<<endl;
}
B(const B& objb)
{
x=new char[strlen(objb.x)+1];
if(*x)
strcpy(x,objb.x);
else
cout<<"memory allocation failure"<<endl;
}
void showx()
{
cout<<"value of x from the object of class
B="<<x<<endl; }
~B(){
try{
if(*x)
delete []x;
else throw 2;
}catch(...){
cout<<"memory already deleted"<<endl;
}
}
};
class A{
char *y;
B *b;
public:
A(char *c="xxx",char *d="bbb"):y(new char[strlen(c)+1]),b(new
B(d)){
try{
// y=new char[strlen(c)+1];
// b=new B(d);
if(*y||*d)
strcpy(y,c);
else
throw 2;
}catch(...){
cout<<"memory allocation failure";
}
}
void showy()
{
cout<<"value of y from the object of class
B="<<y<<endl;
b->showx();
}
~A(){
try{
if(*y)
delete []y;
else throw 2;
/* if(*b)
delete []b;
else throw 2;
*/
}catch(...){
cout<<"memory already deleted in A's
destructor"<<endl;
}
}
};
main()
{
A a("Hello","Gates");
a.showy();
}
compiling: g++ -g -o les9_5 les9_5.C
executing: ./les9_5
output:
value at x
Gates
Constructor B(char *y="xxx")
memory allocation failure
value of y from the object of class B=Hello
value of x from the object of class B=