Thread: Question on dynamic multidimensional arrays

  1. #1
    Registered User
    Join Date
    Sep 2010

    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.
    #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;
         menuSelection = menuSelect();
    int numOfCards(char fileName[])
        fstream cardDB;;
        int count=0;
        char temp[100];
                                        cardDB >> ws;
                                        cardDB.getline(temp, 100);
                                        cardDB >> ws;
    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;;
         if (DBFile)
                                        cout <<iter;
                                        DBFile >> year[iter];
                                        DBFile >> price[iter];
                                        DBFile >> ws;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  3. #3
    Registered User
    Join Date
    Sep 2010
    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, 03: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, 10: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