Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 26th, 2005, 06:05 AM
kk
Guest
 
Posts: n/a
Default memory not allocated

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=

  #2  
Old July 26th, 2005, 06:25 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: memory not allocated

* kk:[color=blue]
> 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[/color]

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.
[color=blue]
>
> ------File Name: les9_5.C--------------[/color]

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

[color=blue]
> #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];[/color]

'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.

[color=blue]
> if(*x)[/color]

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.


[color=blue]
> strcpy(x,y);[/color]

Use std::string instead.

[color=blue]
> else
> {
> cout<<"value at x\t"<<x<<endl;
> cout<<y<<endl;//checkig formal
> parameter[/color]

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

[color=blue]
> throw 1;[/color]

Use standard exception classes.

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

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;

[color=blue]
> }
> }[/color]


[snip][color=blue]
>
> main()[/color]

'main' must have result type 'int'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old July 26th, 2005, 04:15 PM
Howard
Guest
 
Posts: n/a
Default Re: memory not allocated


"kk" <kirancareers@gmail.com> wrote in message
news:1122353643.642452.256760@g44g2000cwa.googlegr oups.com...
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


  #4  
Old July 26th, 2005, 04:55 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: memory not allocated

kk wrote:
[color=blue]
> class B{
> char *x;
> public:
> B(char *y="xxx"):x(new char[strlen(y)+1]){
> try{
>
> if(*x)[/color]

*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.
  #5  
Old July 26th, 2005, 08:05 PM
red floyd
Guest
 
Posts: n/a
Default Re: memory not allocated

Ron Natalie wrote:[color=blue]
> New as you have written it should throw bad_alloc
> on failure.[/color]

I suspect OP is using VC6, based on an earlier discussion thread.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles