How to delete an array of pointers?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: How to delete an array of pointers?

  1. #1
    Join Date
    Dec 2006
    Posts
    2

    How to delete an array of pointers?

    Good day!

    Offtopic:
    I am a student. And I got really used to java. So when it comes to C++ homeworks, situation gets very tricky.

    So here is the code:
    Code:
    #include <iostream>
    //
    // 
    //
    using namespace std;
    
    struct test_ar {
        int *val;
        double *val2;
        test_ar() {
    	val = NULL;
    	val2 = NULL;
        }
        ~test_ar() {
    	delete val;
    	delete val2;
    	cout << "test_ar destroyed" << endl;
        }
    };
    
    int
    main(int argc, char** argv) {
        test_ar **test = new test_ar*[10];
        int x = 10;
        double y = 5.1;
        for (int c = 0; c < 5; c++) {
    	test[c] = new test_ar;
    	test[c]->val = &x;
    	test[c]->val2 = &y;
        }
        for (int c = 5; c < 10; c++) {
    	test[c] = NULL;
        }   
        delete[] test;
        
        return (EXIT_SUCCESS);
    }
    It works fine, but I need to get test_ar destructor working, when I delete an array of pointers to test_ar; When console starts, I don't see anything like test_ar destroyed; Am I doing something wrong?

    Thanks, Andrey;

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    you did it correctly. But you did not delete the memory properly. For this construct, youll need to loop over the inner pointers and delete them first (the actual objects).

    so where you had it assigned to null in that second loop, instead use a delete command.

  3. #3
    Join Date
    Dec 2006
    Posts
    2
    I changed test[i] = NULL to [I]delete test; , but nothing happend.
    But then I chenged the lest part of the code
    delete[] test; into
    Code:
        
    for (int i = 0; i < 10; i++) {
    	if (test[i] != NULL)
    	    delete[] test[i];
        }
    and I got this in the console:
    test_ar destroyed
    test_ar destroyed
    test_ar destroyed
    test_ar destroyed
    test_ar destroyed
    test_ar destroyed
    test_ar destroyed
    Segmentation fault (core dumped)
    [Press Enter to close window]

    after some debugging, I found out that
    delete[] test[0]; instead of
    Code:
        
    for (int i = 0; i < 10; i++) {
    	if (test[i] != NULL)
    	    delete[] test[i];
        }
    gives the same result.

    Why I see test_ar destroyed 7 times instead of 5(or 1)? And how could I fix the
    Segmentation fault (core dumped) message?

    Maybe this could be helpful information:
    I am using linux, and as IDE I use netbeans.

  4. #4
    Join Date
    Dec 2006
    Posts
    16
    you did this?
    :
    ...
    for (int i = 0; i < 10; i++) {
    if (test[i] != NULL)
    delete test[i];
    }
    delete[] test;
    ...

    test[i] is not an array so you can't use delete[] test[i]
    Last edited by blackettle76; 12-13-2006 at 04:53 AM.

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    you repeatedly deleted the same node when you put it to [0] -- deletion does NOT set the value to null, it just gives the memory back to the OS. So.. you tried to delete memory you do not own (anymore) the second time in that loop.

Similar Threads

  1. Reversing an Array
    By Tmcclain in forum Java
    Replies: 7
    Last Post: 02-13-2009, 10:57 PM
  2. delete 2D array - Created Dynamically
    By PoojaPatel in forum C++
    Replies: 22
    Last Post: 02-17-2008, 10:34 AM
  3. Array newbie - some pointers please
    By pcdj in forum Java
    Replies: 3
    Last Post: 02-19-2006, 09:37 AM
  4. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM
  5. help reg creating ms access file
    By vbcoder in forum VB Classic
    Replies: 9
    Last Post: 04-28-2005, 12:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center