Question on dynamic multidimensional arrays

This is a discussion on Question on dynamic multidimensional arrays within the C++ Programming forums, part of the General Programming Boards category; I have been working on this code as part of an assignment for a class. With the code shown below ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Question on dynamic multidimensional arrays

    I have been working on this code as part of an assignment for a class. With the code shown below when I try to delete the allocated memory for the two dimensional arrays the program will hang for 10-15 seconds. I realize there are sections of this code missing and it isn't the prettiest thing in the world. I would like to stick with the pointer arrays rather than using vectors for this because of my class as well. I can post the code in it's entirety if that would be more beneficial. If anyone can tell me what I've done wrong here I'd greatly appreciate. Thanks in advance for your help.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    char **pName;
    int *year;
    char **bName;
    char **cardNum;
    char **teamName;
    float *price;
    char **note;
    
    void menu();
    int numOfCards(char fileName[]);
    void allocArrMem(char fileName[]);
    void deallocArrMem();
    void getDBInfo(char fileName[]);
    
    
    void menu()
    {
         int menuSelection;
         char fileName[256];
         cout << "Please enter the database filename:";
         cin >> fileName;
         cout << endl;
         allocArrMem(fileName);
         menuSelection = menuSelect();
         getDBInfo(fileName);
         deallocArrMem();
    }  
    
    int numOfCards(char fileName[])
    {
        fstream cardDB;
        cardDB.open(fileName);
        int count=0;
        char temp[100];
        if(cardDB)
        {
                    while(!cardDB.eof())
                    {
                                        cardDB >> ws;
                                        cardDB.getline(temp, 100);
                                        count++;
                                        cardDB >> ws;
                    }
        }
        cardDB.close();
        return(count);
    }
                                        
    void allocArrMem(char fileName[])   
    {
              
         int cardCount; 
         int i;
         cardCount = numOfCards(fileName);
         if (cardCount)
         {
                       pName=new char*[26]; 
                       for(i=0; i<26; ++i)
                       pName[i]=new char[cardCount];
                            
                       year=new int[cardCount]; 
                       
                       price=new float[cardCount];
                       
                       bName=new char*[16]; 
                       for(i=0; i<16; ++i)
                       bName[i]=new char[cardCount];
                       
                       cardNum=new char*[6]; 
                       for(i=0; i<6; ++i)
                       cardNum[i]=new char[cardCount];
                       
                       teamName=new char*[21]; 
                       for(i=0; i<21; ++i)
                       teamName[i]=new char[cardCount];
                       
                       note=new char*[11]; 
                       for(i=0; i<11; ++i)
                       note[i]=new char[cardCount];
         }           
    }
    
    void deallocArrMem()
    {
         if(pName && bName && cardNum && teamName && note && year && price)
         {
                  int t;
                   for(t = 0; t < 26; ++t)
                   delete []pName[t];
                   delete []pName;
                   
                   delete []year;
        
                   delete []price;
    
                   for(t = 0; t < 16; ++t)
                   delete []bName[t];
                   delete []bName;
                   for(t = 0; t < 6; ++t)
                   delete []cardNum[t];
                   delete []cardNum;
                   
                   for(t = 0; t < 21; ++t)
                   delete []teamName[t];
                   delete []teamName;
                   
                   for(t = 0; t < 11; ++t)
                   delete []note[t];
                   delete []note;
         }
    }
    void getDBInfo(char fileName[])
    {
         int iter;
         fstream DBFile;
         DBFile.open(fileName);
         iter=0;
         
         if (DBFile)
         {
                    while(DBFile.good())
                    {
                                        cout <<iter;
                                        DBFile.get(pName[iter],26);
                                        DBFile >> year[iter];
                                        DBFile.get(bName[iter],16);
                                        DBFile.get(cardNum[iter],6);
                                        DBFile.get(teamName[iter],21);
                                        DBFile >> price[iter];
                                        DBFile.get(note[iter],11);
                                        DBFile >> ws;
                    }
         }
         DBFile.close();
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    pName=new char*[26];
    for(i=0; i<26; ++i)
    pName[i]=new char[cardCount];

    This is the wrong way round, you've made pName[26][cardCount]

    What you seem to need is pName[cardCount][26] - names for a number of cards, each name up to 26 characters in length.

    Do you know about structures yet?
    This can be reduced to one allocation if you do.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    Thanks for the tip, I eventually figured out I had it backwards and finished. As far as structures go, yes I know it would have been much easier that way. If I had been writing it for myself it would have been written very differently but the professor wanted things done in that manner. Thanks again for the help. Problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick question about dynamic arrays.
    By Lawn Gnomusrex in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2008, 02:22 PM
  2. dynamic arrays
    By sokermaniac in forum C++ Programming
    Replies: 34
    Last Post: 05-12-2008, 12:41 PM
  3. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  5. initialize dynamic arrays in contructors??
    By samual_van in forum C++ Programming
    Replies: 11
    Last Post: 05-03-2005, 08:29 PM

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