Thread: delete array of arrays

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    delete array of arrays

    Code:
    while(!fin.getline(info, 500).eof() )
    		{
    		arrayoffeat = new char * [10];
    		*arrayoffeat = new char[100];
    			
    		        parseinputfromfile(info, arrayoffeat)
    		        myclass[i].setfeat(arrayoffeat);
    
    		delete [] *arrayoffeat;
    		delete [] arrayoffeat;
    		++i;
    		}
    Hey there! I'm having trouble getting my memory to deallocate. pareinputfromfile function just goes through info, parses the information and loads up arrayoffeat. Then myclass is just an array of classes that sets the features into a private data structure. My problem is on outputting the array of characters to stdout. I'm getting garbage from the previous read in. So, its seems like I need to deallocate the memory after every rotation. I have put up my attempt at delete-ing the array of arrays, unfortantly it didn't work. I tried delete [] array[i]; well, it didn't like that.

    Any help would be greatly appriciated!
    Thanks!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your problem isn't in freeing that memory, it's in allocating it. To allocate a 2d array, you need something along these lines:
    Code:
    int **array = new int*[SIZE];
    
    for(int i = 0; i < SIZE; i ++) {
        array[i] = new int[OTHERSIZE];
    }
    Then you free it in reverse order:
    Code:
    for(int i = 0; i < SIZE; i ++) {
        delete [] array[i];
    }
    
    delete [] array;
    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.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Thanks! I was actually allocating the memory like that in my function, I guess I forgot about it while describing the problem. Thanks for the syntax on deleting array of arrays, I know it will come in useful! However, the problem I was having here wasn't deallocating memory, it was a \0 problem, *doh..

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In C++, you generally want to use a vector rather than a dynamic array, and a string rather than a dynamic array of chars (if it is meant to represent a string). Both will grow correctly and clean themselves up automatically.

    >> while(!fin.getline(info, 500).eof() )
    Why not while(!fin.getline(info, 500))? This checks for any failure. There is rarely a failure using getline, but checking for fail state is a more common idiom. Also, if your last line in the input file is not terminated with a newline, it won't be read in with your version but will be with mine.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Thanks for the tips! You have sold me on using your form of getline. I'll have to look into vectors, I havn't used those before! As for strings, i'm actually amazed I forgot about those, thanks for the reminder! I'll have to practice with those too so as not to forget them in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Delete Samllest Array Element
    By ptysell in forum C Programming
    Replies: 5
    Last Post: 11-22-2004, 07:27 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM