473,761 Members | 5,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 23 '05 #1
7 2486
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","Worl d");
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**********@g mail.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","Worl d");
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********@NsO hSaPw.cAaM> did courageously avow:
On 22 Jul 2005 22:09:38 -0700, "kk" <ki**********@g mail.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
dereferencin g 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<<end l;
}
~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<<end l;
b->showx();
}
~A(){
if(*y)
delete []y;
else
cout<<"memory was already deleted"<<endl;
}
};

main()
{
A a("Hello","Worl d");
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(ne w 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<<"Construc tor B(char *y=\"xxx\") "<<endl;
cout<<"memory allocation failure"<<endl;
}
}
B(B *objb):x(new char[strlen(objb->x)+1])
{
cout<<"construc tor 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(ne w 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"<<en dl;
}
}
};

main()
{
A a("Hello","Wils on");
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**********@g mail.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","Worl d");
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
12598
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 about whether we should allow polymorphism. Our system is not embedded and does not need to be as real-time as, say, a pacemaker. But it does involve updating displays based on radar input. So a need for something close to real-time is desired...
5
3335
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 rightType;
4
20819
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 understand what I can do to correct this? The error points to the "MaxKey = DLookup("Expr1", "lkqry_Histry_Max#") + 1" as the breakpoint. thanks,
1
2191
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 SnatchInterNETTD(): '*********************************************************************** ' Internet lookup used to retrieve correct time and date. ' Remove instances of "FUCKING". I could not make this ' post stick unless I changed eBay to eFUCKINGBay. Sorry...
1
2158
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 out the single line of code below, then there is no error. I posted the error message below - it implies that this code is being run at design time, but it bombs due to the fact that it isn't run time. Is there a line of code I could add to only...
1
3602
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, then the compiler (VC7.1) barfed up this lot: c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\time.h(156) : error C2143: syntax error : missing ',' before '*' c:\Program Files\Microsoft Visual Studio .NET
4
2179
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 write the information about user name, filename upload, filesize, date and time of uploading into the log file. (The name of the log file is constructed by Current Year and Current Month in my program). Is there any problems with writing into the...
15
18894
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 the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value with it. the output looks like that "01/01/1900 08:59:00" in that case time is
9
3518
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 has mandatory parameters, I mean, they should not be null nor empty (for strings). So I'd make the validation in the constructor and generate a compile-time error if the validation does not match... Is there a way to achieve this or to specify...
4
2977
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; Error:2/FC:30; 00:15:16 16/03/2009; Error:2/FC:40; 00:15:20 16/03/2009; Error:3/FC:3; 00:20:13 16/03/2009;
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.