Thread: delete - hot to

  1. #1
    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 05:15 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> 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
    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,981
    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
    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, 04: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, 06: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, 11:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM