473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

run time error

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();
}

Jul 23 '05 #1
7 2448
kk wrote:
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.
Jul 23 '05 #2
On 22 Jul 2005 22:09:38 -0700, "kk" <ki**********@gmail.com> did
courageously avow:
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:
if(!*x)
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()"
Jul 23 '05 #3
On Sat, 23 Jul 2005 06:43:30 GMT, Ken Wilson
<ke********@NsOhSaPw.cAaM> did courageously avow:
On 22 Jul 2005 22:09:38 -0700, "kk" <ki**********@gmail.com> did
courageously avow:
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:
if(!*x)


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()"
Jul 23 '05 #4
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
Jul 23 '05 #5
Default User wrote:
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?


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

Jul 24 '05 #6
red floyd wrote:
Default User wrote:
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?


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
Jul 24 '05 #7
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 Wilson wrote:
On 22 Jul 2005 22:09:38 -0700, "kk" <ki**********@gmail.com> did
courageously avow:
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:
if(!*x)


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()"


Jul 25 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
4
by: Christaaay | last post by:
I have been using the code below successfully for almost a year. yesterday, I began getting a run time error 6 (overflow). I am using the code in an Access 2000 database. Can anyone help me...
1
by: MLH | last post by:
I want to change my system time date each time an A97 app is started. Here's how I've been doing it. Am looking for a better way. Sure some of you have researched this. Function...
1
by: Mark | last post by:
I have .aspx code-behinds that inherit from the class below. The code runs just fine, but the form designer bombs at design time when trying to view the .aspx page in VS.NET 2003. If I comment...
1
by: Alfonso Morra | last post by:
Hi I'm compiling some code and need to generate some random numbers. To save time, I decided to use the srand, rand and time functions. My code worked (atleast built fine) until I added time.h,...
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
15
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
9
by: ThunderMusic | last post by:
Hi, I'd like to create a compile time error in my class... maybe there's a way already built in in the framework so I can achieve what I want... I have 2 constructors in my class. One of them...
4
by: ahmurad | last post by:
Dear Xperts, In my database table some fault code type records are inserted and the values are like bellows: Error:2/FC:10; 00:15:13 16/03/2009; Error:2/FC:20; 00:15:15 16/03/2009; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.