memory not allocated

Discussion in 'C++' started by kk, Jul 26, 2005.

  1. kk

    kk Guest

    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=
     
    kk, Jul 26, 2005
    #1
    1. Advertising

  2. * 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.


    > 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?
     
    Alf P. Steinbach, Jul 26, 2005
    #2
    1. Advertising

  3. kk

    Howard Guest

    "kk" <> wrote in message
    news:...
    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
     
    Howard, Jul 26, 2005
    #3
  4. kk

    Ron Natalie Guest

    kk wrote:

    > 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.
     
    Ron Natalie, Jul 26, 2005
    #4
  5. kk

    red floyd Guest

    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.
     
    red floyd, Jul 26, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duncan
    Replies:
    0
    Views:
    405
    Duncan
    Jul 21, 2003
  2. Laura Heinzmann

    How to check memory size allocated to JVM?

    Laura Heinzmann, Feb 16, 2005, in forum: Java
    Replies:
    1
    Views:
    8,229
    John McGrath
    Feb 16, 2005
  3. Kjell Arne Johansen

    How do I know if memory is already allocated?

    Kjell Arne Johansen, Sep 1, 2003, in forum: C++
    Replies:
    8
    Views:
    406
    Kjell Arne Johansen
    Sep 2, 2003
  4. Replies:
    5
    Views:
    663
    Matt Wharton
    Dec 9, 2004
  5. MN
    Replies:
    32
    Views:
    970
    Richard Bos
    Mar 11, 2009
Loading...

Share This Page