Thread: deleting arrays

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    deleting arrays

    Hi,
    I am creating a 2D array. 1 Array will contain pointers to integer arrays.

    Code:
    idArray = new int *[numberOfNodes*2];/*Create array of pointers to the x and y arrays of int*/
    
    for(int i=0;i<numberOfNodes*2;i++) {
    			idArray[i] = new int[simulationTime*2];/*Create array of range and x position*/
    		}
    My question is how do I clean it up afterwards. Will doing a

    delete idArray be enough to clean everything; both the memory allocated for idArray and new int[simulationTime*2]. Thanks
    Amish

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    delete in the opposite order that they were allocated.
    Code:
    for(int i=0;i<numberOfNodes*2;i++) {
    		delete[] idArray[i];
    }
    delete[] idArray;

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> delete[] idArray[i];

    do you need to have the "[]" before the idArray if it's not a whole array you want to delete, like if it's only a certain element of the array?? I thought that was for the entire array ...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It so happens that each element of the array in question is in fact an array, since the array is a 2D array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by twomers
    >> delete[] idArray[i];

    do you need to have the "[]" before the idArray if it's not a whole array you want to delete, like if it's only a certain element of the array?? I thought that was for the entire array ...
    as previously mentioned, whenever you use new[] you have to use delete[]. idArray[i] is an array that was allocated with new[]
    Last edited by Ancient Dragon; 03-06-2006 at 12:29 PM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>is that deleting an element of the array?

    which array -- idArray itself is an array of pointers to integers. Each element of idArray is an array of integers. delete[] idArray[i] is deleteing the ith array of integers.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    cool thanks.
    Amish

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    Heap Corruption Detected

    Hi,
    I got some major error from the debugger of ms vs 2005 with this code:
    Code:
    int **idArray; /*This array contains the pointer to the x and y arrays*/
    int **rangeArray; /*This array contains the range*/
    
    idArray = new int *[numberOfNodes*2];/*Create array of pointers to the x and y arrays of int*/
    memset(idArray,0,sizeof(int *)*(numberOfNodes*2));
    
    rangeArray = new int *[numberOfNodes];/*Create array of pointers to the range of the nodes*/
    memset(rangeArray,0,sizeof(int *)*(numberOfNodes));
    
    for(int i=0;i<numberOfNodes*2;i++) {
      idArray[i] = new int[simulationTime];/*Create arrays of x and y position*/
      memset(idArray[i],0,sizeof(int)*(simulationTime));
    }
    		
    for(int i=0;i<numberOfNodes;i++) {
      rangeArray[i] = new int[simulationTime];/*Create arrays of range*/
      memset(rangeArray[i],0,sizeof(int)*(simulationTime));
    }
    
    *(rangeArray[(currentId+2)/2 - 1]+counter) = range; /*Put the rage in for the time interval*/
    
    *(idArray[currentId]+counter) =  (int) (subInt*cos((direction-0)*PI/180) + *(idArray[currentId]+counter-1)); /*Puts in the x position*/
    
    
    *(idArray[currentId+1]+counter) = (int) (subInt*sin((direction-0)*PI/180) + *(idArray[currentId+1]+counter-1)); /*Puts in the y position*/
    
    counter++;
    The code is basically calculating position and range for different times. CurrentId is always less that numberOfNodes, counter does not go beyond simulationTime.

    To delete the arrays, I used this code:
    Code:
    if(rangeArray != NULL) {
    		
     for(int i=0;i<numberOfNodes;i++) {
      delete[] rangeArray[i];
     }
    	
     delete[] rangeArray;
     rangeArray = NULL;
    }
    
    if(idArray != NULL) {
     for(int i=0;i<numberOfNodes*2;i++) {
      delete[] idArray[i];
     }
    
     delete[] idArray;
     idArray = NULL;
    }
    Whenever the debugger hits the line delete[] rangeArray[i]; I get the following error:
    Debug error
    program ...
    HEAP CORRUPTION DETECTED: after normal block (#128) at 0x003682F0. CRT detected that the application wrote to memory after end of heap buffer.

    I am completely confused. I thought memory was being deallocated at this point. So the program should not be writing any additional stuff to the heap. Hopefully somebody can point me in the right direction for this. Thanks
    Amish

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    delete[] idArray[i];
    Unless idarray is a three dimensional array, which it isn't, you should use this instead:
    Code:
    delete idArray[i];
    delete[] is only for arrays.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Quote Originally Posted by dwks
    Code:
    delete[] idArray[i];
    Unless idarray is a three dimensional array, which it isn't, you should use this instead:
    Code:
    delete idArray[i];
    delete[] is only for arrays.
    idArray is a 2D array so it would make sense to delete the array in one of the dimensions and then delete the array in the second dimension. idArray[i] is in fact an array and not just an element. OR am I wrong. Thanks
    Amish

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    Code:
    delete[] idArray[i];
    Unless idarray is a three dimensional array, which it isn't, you should use this instead:
    Code:
    delete idArray[i];
    delete[] is only for arrays.

    But ... idArray[i] was allocated with new[], so delete[] would be correct. there could be some other unrelated problem in program, such as index out-of-bounds.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your delete code is correct.

    >> CRT detected that the application wrote to memory after end of heap buffer.

    In other words, at some you wrote past the end of your array, probably the rangeArray. Try checking the indexes that are used with these arrays. Make sure they are less than (not equal to) simulationTime for the inner arrays, less than numberOfNodes for the rangeArray, and less than numberOfNodes*2 for the idArray. Use the debugger or output statements, or add an assert on the code you are using to get the indexes.

    For example: assert(((currentId+2)/2 - 1) < numberOfNodes);

    or assert(counter < simulationTime);
    Last edited by Daved; 03-07-2006 at 12:48 PM.

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    An array out of bounds was causing the problem. I was using a for loop with i=0;i<=somenumber;i++
    It should have been i<somenumber. Well the debug version of this program works now. Now time to go see if the release is gonna f...k up Thanks everybody
    Amish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Deleting characters from 2d arrays
    By `firefox in forum C Programming
    Replies: 4
    Last Post: 05-21-2005, 05:18 PM
  4. Deleting Dynamic array's of an object
    By j0hnb in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2004, 03:25 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM