run time error

K

kk

Hi Guys, i am posting a cpp program contains runtime error, memory not
allocated to char *x in class B. while accessing showx function it
doesn't print values. if anybody knows send a reply plz.
thanks in advance
kiran

#include <iostream.h>
#include <string.h>
#include <stdio.h>

class B{
char *x;
public:
B(char *y="xxx"){
cout<<"string of x"<<y<<endl;
x=new char[strlen(y)+1];
if(!*x)
strcpy(x,y);
else
cout<<"memory allocation failure"<<endl;
}

B(B *objb):x(new char[strlen(objb->x)+1])
{
strcpy(x,objb->x);
}
B(const B& objb)
{

x=new char[strlen(objb.x)+1];
strcpy(x,objb.x);
}
void showx()
{
cout<<"value of x from the object of class
B="<<x<<endl;
}
~B(){
if(*x)
delete []x;
}
};
class A{
char *y;
B *b;

public:
A(char *c="xxx",char *d="bbb"){
y=new char[strlen(c)+1];
b=new B(d);
if(!*y||!*d)
{
cout<<"memory allocation failure"<<endl;

}

strcpy(y,c);
}

void showy()
{
cout<<"value of x from the object of class
B="<<y<<endl;
b->showx();
}
~A(){
if(*y)
delete []y;
else
cout<<"memory was already deleted"<<endl;
}
};

main()
{
A a("Hello","World");
a.showy();
}
 
R

red floyd

kk said:
Hi Guys, i am posting a cpp program contains runtime error, memory not
allocated to char *x in class B. while accessing showx function it
doesn't print values. if anybody knows send a reply plz.
thanks in advance
kiran

#include <iostream.h>
#include <string.h>
#include <stdio.h>
Wrong headers:
#include <iostream>
#include <cstring>
#include <cstdio> // but not needed

try making the constructor parameters "const char *".
class B{
char *x;
public:
B(char *y="xxx"){
cout<<"string of x"<<y<<endl;
x=new char[strlen(y)+1];
if(!*x)
strcpy(x,y);
else
cout<<"memory allocation failure"<<endl;
You don't need to check memory allocation failure. new throws
std::bad_alloc in case of allocation failure.
In addition, you're only copying if the first byte of newly allocated
memory is a NUL character. I'm surprised you aren't getting an error
that says "memory allocation failure".

}

B(B *objb):x(new char[strlen(objb->x)+1])
{
strcpy(x,objb->x);
}
B(const B& objb)
{

x=new char[strlen(objb.x)+1];
strcpy(x,objb.x);
}
void showx()
{
cout<<"value of x from the object of class
B="<<x<<endl;
}
~B(){
if(*x)
delete []x;
}
};
class A{
char *y;
B *b;

public:
A(char *c="xxx",char *d="bbb"){
y=new char[strlen(c)+1];
b=new B(d);
if(!*y||!*d)
{
cout<<"memory allocation failure"<<endl;

}

strcpy(y,c);
}

void showy()
{
cout<<"value of x from the object of class
B="<<y<<endl;
b->showx();
}
~A(){
if(*y)
delete []y;
else
cout<<"memory was already deleted"<<endl;
}
};

main()
{
A a("Hello","World");
a.showy();
}

In general, you should try to use std::string instead of dynamic memory
allocation. Also, based on your header usage, I'm assuming you're using
VC6. Upgrade to 7.1 or gcc 3 or later. These are much more Standard
compliant.
 
K

Ken Wilson

Hi Guys, i am posting a cpp program contains runtime error, memory not
allocated to char *x in class B. while accessing showx function it
doesn't print values. if anybody knows send a reply plz.
thanks in advance
kiran

#include <iostream.h>
#include <string.h>
#include <stdio.h>

class B{
char *x;
public:
B(char *y="xxx"){
cout<<"string of x"<<y<<endl;
x=new char[strlen(y)+1];

Your error is coming here:

You are in effect asking, first of all if the string has content by
dereferencing the pointer, and then secondly you negate that, turning
the answer you reasonably expect to be true into false. You should
test to see if the pointer, x, is null == if (x), or if the string has
content other than the null terminator == if (*x)
strcpy(x,y);
else
cout<<"memory allocation failure"<<endl;
}

B(B *objb):x(new char[strlen(objb->x)+1])
{
strcpy(x,objb->x);
}
B(const B& objb)
{

x=new char[strlen(objb.x)+1];
strcpy(x,objb.x);
}
void showx()
{
cout<<"value of x from the object of class
B="<<x<<endl;
}
~B(){
if(*x)
delete []x;
}
};
class A{
char *y;
B *b;

public:
A(char *c="xxx",char *d="bbb"){
y=new char[strlen(c)+1];
b=new B(d);
if(!*y||!*d)
{
cout<<"memory allocation failure"<<endl;

}

strcpy(y,c);
}

void showy()
{
cout<<"value of x from the object of class
B="<<y<<endl;
b->showx();
}
~A(){
if(*y)
delete []y;
else
cout<<"memory was already deleted"<<endl;
}
};

main()
{
A a("Hello","World");
a.showy();
}


Ken Wilson
"Coding, coding, over the bounding main()"
 
K

Ken Wilson

Hi Guys, i am posting a cpp program contains runtime error, memory not
allocated to char *x in class B. while accessing showx function it
doesn't print values. if anybody knows send a reply plz.
thanks in advance
kiran

#include <iostream.h>
#include <string.h>
#include <stdio.h>

class B{
char *x;
public:
B(char *y="xxx"){
cout<<"string of x"<<y<<endl;
x=new char[strlen(y)+1];

Your error is coming here:

You are in effect asking, first of all if the string has content by
dereferencing the pointer, and then secondly you negate that, turning
the answer you reasonably expect to be true into false. You should
test to see if the pointer, x, is null == if (x), or if the string has
content other than the null terminator == if (*x)

Follow-up: You are probably better off testing the pointer, if it is
null, you're okay. If you test the string but the pointer happens to
be null I'm pretty certain you will crash.
strcpy(x,y);
else
cout<<"memory allocation failure"<<endl;
}

B(B *objb):x(new char[strlen(objb->x)+1])
{
strcpy(x,objb->x);
}
B(const B& objb)
{

x=new char[strlen(objb.x)+1];
strcpy(x,objb.x);
}
void showx()
{
cout<<"value of x from the object of class
B="<<x<<endl;
}
~B(){
if(*x)
delete []x;
}
};
class A{
char *y;
B *b;

public:
A(char *c="xxx",char *d="bbb"){
y=new char[strlen(c)+1];
b=new B(d);
if(!*y||!*d)
{
cout<<"memory allocation failure"<<endl;

}

strcpy(y,c);
}

void showy()
{
cout<<"value of x from the object of class
B="<<y<<endl;
b->showx();
}
~A(){
if(*y)
delete []y;
else
cout<<"memory was already deleted"<<endl;
}
};

main()
{
A a("Hello","World");
a.showy();
}


Ken Wilson
"Coding, coding, over the bounding main()"


Ken Wilson
"Coding, coding, over the bounding main()"
 
D

Default User

red floyd wrote:

In general, you should try to use std::string instead of dynamic
memory allocation. Also, based on your header usage, I'm assuming
you're using VC6. Upgrade to 7.1 or gcc 3 or later. These are much
more Standard compliant.

I don't understand what you mean. Do you think that VC++ 6.0 doesn't
support the standard headers?




Brian
 
R

red floyd

Default said:
red floyd wrote:





I don't understand what you mean. Do you think that VC++ 6.0 doesn't
support the standard headers?

It does have the standard headers, but VC6 *does* predate the Standard.
In addition, its template support is horrible. Microsoft didn't
really get close to compliance until 7.1
 
D

Default User

red said:
It does have the standard headers, but VC6 does predate the Standard.
In addition, its template support is horrible. Microsoft didn't
really get close to compliance until 7.1


It does have some known problems, but it's not as bad as all that.
We're still using it and for the most we can write portable code
without too much problem. The tools groups is still evaluating the
newer versions.

The main thing is that "upgrading" to most people is a costly business,
unless Microsoft is handing out free upgrades. g++ isn't such a bad
problem of course.



Brian
 
K

kk

Hi Ken Wilson,
Thanks for your reply.
i changed the code but while compiling and executing with g++ ver 3.2.3
it doesn't allocate memory. and it gives correct output while executing
in .Net and visual slick editor. i am sending the changed code again.


------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","Wilson");
a.showy();
}


compiling: g++ -g -o les9_5 les9_5.C
executing: ./les9_5
output:
value at x
Wilson
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=



Ken said:
Hi Guys, i am posting a cpp program contains runtime error, memory not
allocated to char *x in class B. while accessing showx function it
doesn't print values. if anybody knows send a reply plz.
thanks in advance
kiran

#include <iostream.h>
#include <string.h>
#include <stdio.h>

class B{
char *x;
public:
B(char *y="xxx"){
cout<<"string of x"<<y<<endl;
x=new char[strlen(y)+1];

Your error is coming here:

You are in effect asking, first of all if the string has content by
dereferencing the pointer, and then secondly you negate that, turning
the answer you reasonably expect to be true into false. You should
test to see if the pointer, x, is null == if (x), or if the string has
content other than the null terminator == if (*x)
strcpy(x,y);
else
cout<<"memory allocation failure"<<endl;
}

B(B *objb):x(new char[strlen(objb->x)+1])
{
strcpy(x,objb->x);
}
B(const B& objb)
{

x=new char[strlen(objb.x)+1];
strcpy(x,objb.x);
}
void showx()
{
cout<<"value of x from the object of class
B="<<x<<endl;
}
~B(){
if(*x)
delete []x;
}
};
class A{
char *y;
B *b;

public:
A(char *c="xxx",char *d="bbb"){
y=new char[strlen(c)+1];
b=new B(d);
if(!*y||!*d)
{
cout<<"memory allocation failure"<<endl;

}

strcpy(y,c);
}

void showy()
{
cout<<"value of x from the object of class
B="<<y<<endl;
b->showx();
}
~A(){
if(*y)
delete []y;
else
cout<<"memory was already deleted"<<endl;
}
};

main()
{
A a("Hello","World");
a.showy();
}


Ken Wilson
"Coding, coding, over the bounding main()"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top