memory not allocated

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])"<<end­l;
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=
 
A

Alf P. Steinbach

* 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

Uhuh. This must be the worst formatted code I've ever responded to here.
Please do something about that, and DON'T post with MIME-coding.
------File Name: les9_5.C--------------

To help your tools help you, use a filename that's recognized as C++ source
code.

#include <iostream>
#include <string.h>
using namespace std;
class B{
char *x;
public:
B(char *y=3D"xxx"):x(new char[strlen(y)+1]){
try{


// x=3Dnew char[strlen(y)+1];

'x' is already initialized. This new allocation means you're discarding the
pointer to the previously allocated memory, without deallocating it. You're
leaking memory.


Undefined Behavior.

The program can do anything here.

You're referencing memory -- what x points to -- that hasn't been
initialized.

Anyway in standard C++ the 'if' will not be executed if the allocation
fails.

If the allocation fails you get a std::bad_alloc exception, not a null
pointer.


strcpy(x,y);

Use std::string instead.

else
{
cout<<"value at x\t"<<x<<endl;
cout<<y<<endl;//checkig formal
parameter

Pass that information via the exception, e.g. a std::runtime_error, instead
of doing i/o down in your classes.


Use standard exception classes.

}
}catch(...){
cout<<"Constructor B(char *y=3D\"xxx\") "<<endl;
cout<<"memory allocation failure"<<endl;

Here you forgot to rethrow the exception, which means the calling code may
go on to use a B-object that isn't really usable.

Add:

throw;

}
}

[snip]

main()

'main' must have result type 'int'.
 
H

Howard

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];

A few things:

1) you've commented out the allocation above, and replaced it with an
initializer list. Why put the "new" in the initializer, but keep this
useless code below which checks if x is allocated or not? In a conforming
compiler, new throws an exception if it fails. Why do you want all this
code below? If the exception happens, it will happen in the initializer
list above, before you enter the try, where you actually _could_ catch it.

2) Why are you BOTH checking if the pointer is nil AND trying
(unsuccessfully) to catch the allocation exception? Remove the if statement
test.

3) What if y is nil? Your code should probably be testing if y is nil (not
if x is nil), and if it's not, THEN allocate memory for x and copy y to it.
(Otherswise I assume x should be nil?)

4) Suppose in your code that you did catch an exception somehow. You're
allowing the constructor to continue, reporting via cout that an error
occurred. But there's still a problem, in that x was never properly
allocated. How's the rest of your program going to like that? If you want
to report an exception via cout, that's fine, but you need to be sure you
either re-throw the exception or else take some appropriate action to make
sure your object is in a viable state.

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

-Howard
 
R

Ron Natalie

kk said:
class B{
char *x;
public:
B(char *y="xxx"):x(new char[strlen(y)+1]){
try{

if(*x)

*x doesn't have a deterministic value. Even if the language
default initialized it, *x would be 0. I suspect you really
were trying to see do
if(x)
However, there is no way on a conforming compiler that the
value of your new expression would have returned a null
pointer. New as you have written it should throw bad_alloc
on failure.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top