delete - hot to

This is a discussion on delete - hot to within the C++ Programming forums, part of the General Programming Boards category; i have a set of objects of class A as a data member of an object of class B using ...

  1. #1
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    delete - hot to

    i have a set of objects of class A as a data member of an object of class B using dynamic memory allocation. In A i got strings with dma.

    delete [] A;

    gives me a segmentation fault...

    it works just fine if A is a string, what is the difference in what i'm doing?

    Code:
    A* tmpacct;
    		tmpacct = new A[arraySize + s];
    		for(int i = 0; i<size; i++){
    			tmpacct[i] = savings[i];
    		}
    		// CRAPS OUT HERE ____________________________
    		delete [] savings;
    		savings = new A[arraySize + s];
    		for(int i = 0; i<size; i++){
    			savings[i] = tmpacct[i];
    		}
    		delete [] tmpacct;
    
    		arraySize = arraySize + s;
    Last edited by rox; 04-01-2004 at 04:15 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    >> In A i got strings with dma
    There could be problems in A's constructor.
    There could be problems in A's destructor.
    If A doesn't have an overloaded equals operator (and copy constructor) then you could be performing shallow copies of A, which would be bad is A is calling delete in it's destructor.

    Use this opportunity to start learning how to use your debugger to track down these issues.

    gg

  3. #3
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    ok, i've pinpointed it to this:

    Code:
    class A {
    
       char * cr; //No memory allocation yet.
      
    public:
       //...............
       ~A();
    };
    and it craps out here:

    Code:
    A::~A() {
    	if(cr) {
                    // on the next line : 
    		delete [] cr;
    	}
    }
    now, cr is just a string of characters... this looks like the book, i'm confused...

    what debugger? I compile with c++ on aix (as per requirements)...

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    Quote Originally Posted by Codeplug
    If A doesn't have an overloaded equals operator (and copy constructor) then you could be performing shallow copies of A, which would be bad if A is calling delete in it's destructor.
    Here's a program entitled "When Shallow Copy'n Goes Wrong".
    Run it, it should be educational...
    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
        char *str;
    
    public:
        A() 
        {
            str = new char[100];
            
            cout << "constructor  -  new 0x" 
                 << hex << (int)str << dec << endl;
        }//constuctor
    
        ~A()
        {
            cout << "destructor - delete 0x"
                 << hex << (int)str << dec << endl << flush;
    
            delete[] str;
        }//destructor
    };//A
    
    int main()
    {
        A a, b;
    
        a = b;
    
        return 0;
    }//main
    >> what debugger?
    gdb

    gg

  5. #5
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    great, that gdb is amazing, i had no ideea there's a debugger for prompt.
    I was under the impression that only heavy-graphical-interface applications have a debugger.
    (you learn something every day).

    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 03:30 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 05:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 10:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21