Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > memory not allocated

Reply
Thread Tools

memory not allocated

 
 
kk
Guest
Posts: n/a
 
      07-26-2005
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")(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)(new char[strlen(objb->x)+1])
{
cout<<"constructor B(B *objb)(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=

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-26-2005
* 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")(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.


> if(*x)


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.


> throw 1;


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

--
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?
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      07-26-2005

"kk" <> wrote in message
news: 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")(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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      07-26-2005
kk wrote:

> class B{
> char *x;
> public:
> B(char *y="xxx")(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.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      07-26-2005
Ron Natalie wrote:
> New as you have written it should throw bad_alloc
> on failure.


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

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why 50% of allocated memory is not freed? MN C Programming 32 03-11-2009 03:40 PM
How to check memory size allocated to JVM? Laura Heinzmann Java 1 02-16-2005 04:37 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
How do I know if memory is already allocated? Kjell Arne Johansen C++ 8 09-02-2003 06:12 AM
Duplicate memory allocated by IndexedTriangleArray Duncan Java 0 07-21-2003 07:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57