delete operation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey22
    New Member
    • Feb 2007
    • 105

    delete operation

    I have created a pointer to a data in a class A and passing this pointer to a function in another class B.when I try to delete this pointer in class A I get run time exception.
    But if I dont delete the object of class B I dont get any error it runs fine.But I have to delete the object of class B.I have illustrated theexample..Cou ld anyone help with it?

    [CODE=cpp]//func2.h

    class b
    {
    private:
    float * data2;
    b(float *);
    ~b();
    test2();
    };

    //func2.cpp
    void b::b(float *_data2)
    {
    data2=_data2;
    }

    void b::~b()
    {
    if(data2!=NULL) { delete[] data2; data2=NULL; }
    }

    //func1.h

    class a
    {
    private:
    float *data1;
    a();
    ~a();
    test();
    };

    //func1.cpp
    void a::a()
    {
    data1=NULL;
    }

    void a::~a()
    {
    if(data1!=NULL) { delete[] data1; data1=NULL; }
    }

    void a::test()
    {
    b *obj=NULL;
    obj=new b(data1);
    delete b;
    b=NULL;
    }[/CODE]


    Thanks
    Last edited by Ganon11; May 5 '08, 05:05 PM. Reason: Please use the [CODE] tags provided.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It looks like your constructor and destructor are private. They should be public.

    Next the constructor takes a float* argument and you jusy copy the argument to the class data member. Then you delete the class data member in the destructor.

    If the float* argument points to a float on the stack, you will crash at run time trying to delete a variable you didn't allocate.

    You need to rewrite your constructor to allocate a new float on the heap and put that address in your class data member. Now you can safely delete in the destructor.

    [code=cpp]
    void b::b(float *_data2)
    {
    data2 = new float(*_data2);
    }
    [/code]

    Comment

    • mickey22
      New Member
      • Feb 2007
      • 105

      #3
      Thank you ..using new operator solved my problem.

      One question: is it not required to initialise data2 in class B to NULL before using new operator?

      If yes can I intialise in this way :
      data2=NULL;
      data2=new float(*_data2);

      Comment

      • mickey0
        New Member
        • Jan 2008
        • 142

        #4
        I see many strange things (for me):
        1. the underscore _, normally is used in the data member of a class and not as member parameters (the opposite you're doing);
        2. in the destructor check if _data != NULL, is unuseful;
        3. I never heard that destructor/constructor have to return a type (void). Maybe some compiler accepts it but not mine
        4. It's a good practice write class name with "uppercase" ( "class B" and not "class b")
        Originally posted by mickey22
        One question: is it not required to initialise data2 in class B to NULL before using new operator?
        correct; it isn't required;
        but I initialize it like this:
        Code:
        class B () {
          float* _data;
        public:
              B(float* data) : _data( new double(*data)) { }
        };
        Originally posted by mickey22
        If yes can I intialise in this way :
        data2=NULL;
        data2=new float(*_data2);
        yes, you can; it's the same that 'weaknessforcat s' wrote.

        Comment

        • mickey22
          New Member
          • Feb 2007
          • 105

          #5
          Hi all,

          When I initialise in this way,

          b::b(float *_data2)
          {
          data2 = new float(*_data2);
          }

          copying of the data from class A is not done properly.data2 contains some weird values.

          So I tried to do as:

          b::b(float *_data2,int _x,int _y)
          {
          x=_x;
          y=_y;
          data2=new float[x*y];
          data2=_data2;
          }

          I get a runtime exception when I try to run this but the copying of data is done properly.
          I am trying to copy data say float *data1 of one class A to a member variable float *data2 in another class B.
          Could anyone help me with this please?
          Thanks.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by Mickey22
            b::b(float *_data2)
            {
            data2 = new float(*_data2);
            }
            You don't show your main(). However, this is correct assuming _data2 points to a valid float.

            BTW: The underscore does not mean a member variable. Member variables are identifed by using the this pointer:

            [code=cpp]
            b::b(float *data2)
            {
            this->data2 = new float(*data2);
            }
            [/code]

            Originally posted by Mickey22
            b::b(float *_data2,int _x,int _y)
            {
            x=_x;
            y=_y;
            data2=new float[x*y];
            data2=_data2;
            }
            data2 is a pointer. When you assign _data2 (also a pointer) to data2 all that happens is a) the address in _data2 is copied to data2 and b) the address returned by new has been overwritten resulting in a leak.

            Further, why allocate an array of float to hold the value of a single float?

            This is a completely erronous approach.

            How about posting code where the error occurs?

            Comment

            • mickey22
              New Member
              • Feb 2007
              • 105

              #7
              yes I have observed there is memory leak when I copy the adresses of the pointers and delete them in class A and class B.Thats where I get an error when I try to peform delete in class A.
              delete[] data2;

              How can I copy the values pointed by data2 to data1 point to the same values having data1 and data2 as different adresses so that I can delete them in both the classes?Can you suggest me the way?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by Mickey22
                How can I copy the values pointed by data2 to data1 point to the same values having data1 and data2 as different adresses so that I can delete them in both the classes?Can you suggest me the way?
                If I read this correctly, you are wanting data2 and data1 to point to the same value? And then, when you delete in the destructors there are no problems.

                If this is what you want, then you can't use pointers in your classes. Instead, you use a reference counted handle object. There is one here complete with code that you can use: http://bytes.com/forum/thread651599.html.

                Comment

                • mickey22
                  New Member
                  • Feb 2007
                  • 105

                  #9
                  Actually I have solved using the copy function memcpy and just copied the data pointed by data1 to pointer data2.Now I dont have any problem in the delete operations .

                  thanks a lot .

                  Comment

                  • Airslash
                    New Member
                    • Nov 2007
                    • 221

                    #10
                    Can't you just copy the values of your pointers by dereferencing them ?

                    Like this :

                    [code=c]
                    Class A {
                    public:
                    A();
                    float* var_A_;

                    }

                    Class B {
                    public:
                    B();
                    copy(A obj);

                    private:
                    floar* var_B_;
                    }

                    B::copy(A* obj) {
                    Float* var1 = A->var_A_;
                    Float* var2 = *var1;
                    }
                    [/code]

                    Just a quick thouht about the situation, you assign the value of the pointer in A (not the Adress) to the new variable.

                    Another trick would be to use the copyconstructor of your float type.
                    Float* var2 = new Float(var1);

                    anyways just my thought

                    Comment

                    Working...