Segmenation Faults when using new and delete

This is a discussion on Segmenation Faults when using new and delete within the C++ Programming forums, part of the General Programming Boards category; Ok the red lines cause faults when they are not commented out. Can anybody fix this? Code: //copy constructor heap::heap(const ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10

    Segmenation Faults when using new and delete

    Ok the red lines cause faults when they are not commented out.
    Can anybody fix this?

    Code:
    //copy constructor
    heap::heap(const heap &aHeap)
    {
      delete [] heapArr;                                                                                                                        
      heapArr = NULL;                                                                                                                           
    
      cout << heapArr <<endl;
      heapArr = new HeapNode[aHeap.maxSize];
      if (heapArr != NULL)
      {
        maxSize = aHeap.maxSize;
        curSize = aHeap.curSize;
        for (int a = 0; a <= curSize; a++)
        {
          heapArr[a] = aHeap.heapArr[a];
        }
      }
      else
        maxSize = 0;
    }
    
    
    bool heap::buildHeap(HeapNode *n, const int nodes)
    {
    
      //delete previous heap if one exists                                                                                                        
      if(curSize != 0)
      {
        delete [] heapArr;
        heapArr = NULL;
        curSize = 0;
        maxSize = 0;
      }
        cout << "reallocate" <<endl;
        cout << heapArr <<endl;
      heapArr = new HeapNode[nodes];
      cout << "after reallocate " <<endl;
      if (heapArr != NULL)
      {
        maxSize = nodes;
        for (int a = 1; a <= maxSize; a++)
        {
          cout << "begin copy" <<endl;
          heapArr[a] = n[a];
          curSize = a;
          percDown(a);
        }
        return true;
      }
      else
        maxSize = 0;
      return false;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for (int a = 1; a <= maxSize; a++)
    > {
    > cout << "begin copy" <<endl;
    > heapArr[a] = n[a];
    Arrays start at 0. Here you are going one past the end of the array, as 0 is the first element, maxSize-1 is the last element. You probably have the same problem in your constructor.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    It's a constructor, so by definition, nothing exists prior to being called.

    Code:
    //copy constructor
    heap::heap(const heap &aHeap)
    {
      delete [] heapArr;  // remove this line.
      heapArr = NULL;     // this does nothing useful. but it isn't harmful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10
    Quote Originally Posted by Salem
    It's a constructor, so by definition, nothing exists prior to being called.

    Code:
    //copy constructor
    heap::heap(const heap &aHeap)
    {
      delete [] heapArr;  // remove this line.
      heapArr = NULL;     // this does nothing useful. but it isn't harmful.
    that what i thought about the constructor. I was using my professors code for a queue and she had it there( I think it caused a fault when i ran it). I sent her an email asking if I really need it already and am still waiting for a response.

    Thanks for the reasurance about whether or not i needed that code

    Quote Originally Posted by swoopy
    > for (int a = 1; a <= maxSize; a++)
    > {
    > cout << "begin copy" <<endl;
    > heapArr[a] = n[a];
    Arrays start at 0. Here you are going one past the end of the array, as 0 is the first element, maxSize-1 is the last element. You probably have the same problem in your constructor.
    You're right arrays start at 0. I forgot that because for heaps my class has been taught to use 1 for the start. I needed something like:
    Code:
     maxSize = nodes;
        for (int a = 0; a < maxSize; a++)
        {                                                                                                                                         
          cout << "begin copy" <<endl;
          heapArr[a+1] = n[a];
          curSize = a+1;
          percDown(a+1);
        }
    Thanks for the reminder.

    Now all I just need to get it to organize properly.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It should also be mentioned that on new compilers, testing the return value of new is unnecessary, as new either returns a valid pointer or throws an exception.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

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