Can't delete memory I own

This is a discussion on Can't delete memory I own within the C++ Programming forums, part of the General Programming Boards category; Code: int **loop; . . . loop = new int* [size]; . . . for (i = 0; i < ...

  1. #1
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368

    Can't delete memory I own

    Code:
    int **loop;
    .
    .
    .
    loop = new int* [size];	
    .
    .
    .
    for (i = 0; i < size; i++)
    		loop [i] = new int [size + 1];
    .
    .
    .
    for (i = 0; i < size; i++)
    		delete loop [i];
    Worked fine until I put in the deallocation stuff at the bottom. Now it crashes. WTF?
    Last edited by samGwilliam; 04-04-2005 at 10:07 PM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Check the the [] operator.

    delete [] data[nIndex];

    Kuphryn

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just nitpicking: when you allocate memory, you don't exactly own it... you're asking the Operating system to lend you the memory for a while... The operating system can always take your memory away if it decides you're using it wrong.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You need to check to see if what you are deleting is an array. When you use:

    new []

    to create an array, then you use

    delete []

    to delete the array. Since every loop[i] is an array, you need to:

    delete [] loop[i];

    You could have some multidimensional array:

    loop[i][j][k][l]

    and if each element of that array were a dynamically allocated array itself, then you would

    delete [] loop[i][j[k][l];
    Last edited by 7stud; 04-04-2005 at 11:17 PM.

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Still doesn't work. I'll just keep taking more and more memory without giving it back until the OS falls over. It's obviously what MS want or else they wouldn't release a compiler that blatantly disregards your explicit instructions.
    Last edited by samGwilliam; 04-04-2005 at 11:19 PM.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It's obviously what MS want or else they wouldn't release a compiler that blatantly disregards your explicit instructions.
    So try it on gcc under Unix.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    This compiles and runs fine in VC6:
    Code:
    #include<iostream>
    using namespace std;
    
    class Apple
    {
    public:
    	~Apple()
    	{
    		cout<<"destructor called"<<endl;
    	}
    };
    
    int main()
    {
    
    int size = 2;
    Apple **loop;
    
    loop = new Apple* [size];	
    
    for (int i = 0; i < size; i++)
    		loop [i] = new Apple [size + 1];
    
    for (i = 0; i < size; i++)
    		delete [] loop [i];
    
    
    
    	return 0;
    }
    The destructor is called 6 times, which is what you would expect for a 2x3 array.

  8. #8
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Then I don't know. It just falls over on mine...

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the following worked in Dev-C++ (two slight modifcications)
    Code:
    #include<iostream>
    using namespace std;
    
    class Apple
    {
    public:
      ~Apple()
      {
        cout<<"destructor called"<<endl;
      }
    };
    
    int main()
    {
    
    int size = 2;
    Apple **loop;
    
    loop = new Apple* [size];
    
    for (int i = 0; i < size; i++)
        loop [i] = new Apple [size + 1];
    
    for (int i = 0; i < size; i++)  //added int in front of the first i
        delete [] loop [i];
    
    
        std::cin.get(); //put this in to wait for user input
      return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You haven't really showed us your code yet, so you might want to consider posting it. Did you cut and paste the code I posted exactly as I posted it and try to run it, but it didn't work?
    Last edited by 7stud; 04-05-2005 at 12:00 AM.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Is it just me, or has everyone forgotten to delete[] the array of pointers?
    Code:
    for (int i = 0; i < size; i++)  //added int in front of the first i
        delete [] loop [i];
    
    delete[] loop;
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Quote Originally Posted by 7stud
    You haven't really showed us your code yet, so you might want to consider posting it. Did you cut and paste the code I posted exactly as I posted it and try to run it, but it didn't work?
    Found the problem - the program didn't necessarily need all the arrays. Every iteration of the main loop, a condition would possibly cause the program to break out of the loop if it was prematurely complete (a possibility with this algorithm) but afterwards it would attempt to delete ALL arrays (even those that weren't allocated due to premature exit).



    It was late....

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Is it just me, or has everyone forgotten to delete[] the array of pointers?
    Ooops.

    Quote Originally Posted by me
    When you use:

    new []

    to create an array, then you use

    delete []

  14. #14
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Quote Originally Posted by Hunter2
    Is it just me, or has everyone forgotten to delete[] the array of pointers?
    Code:
    for (int i = 0; i < size; i++)  //added int in front of the first i
        delete [] loop [i];
    
    delete[] loop;
    Yes, my code does that but I didn't bother putting it up here as it wasn't relevant to my problem.

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    a condition would possibly cause the program to break out of the loop if it was prematurely complete (a possibility with this algorithm) but afterwards it would attempt to delete ALL arrays
    Ah, dontcha just hate dynamic memory..
    *upbeat music begins*
    Use std::vector!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM
  4. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 05:14 PM
  5. multiple indirection...
    By doubleanti in forum C++ Programming
    Replies: 7
    Last Post: 08-31-2001, 11:56 AM

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