DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Dynamically allocating a 2d array

  1. #1
    Join Date
    Oct 2005

    Dynamically allocating a 2d array

    As part of a program i have a function that takes in a word and saves it into a 2d array. Before it saves the word it checks to make sure there is room in the array. If the number of words in the array is equal to the current size of the row dimension then more space needs to be allocated. The problem occurs after the new bigger 2d array is created and i want to assign the address of this array to the original pointer to the smaller array.

    The parameters to the procedure are
    word = a string holding the new word to be saved
    results = a 2d array holding all previously saved words
    numWords = the number of words currently in the array
    maxWords = the maximum number of words for which there is space. i.e the size of the row parameter to results.

    RMAX_LEN is #define - ed earlier in the program to 60

    Any help is appreciated.

    void saveWord(string &word, char results[][RMAX_LEN], int &numWords, int &maxWords)
            unsigned int i(0), j(0);
            //First check there is room for another word
            if(numWords == maxWords){
                    //put the address of the array of char* in tempResults
                    char **tempResults= new char*[maxWords*2];
                    //Each element of temp results holds a pointer to a char array
                    for(i = 0; i < (maxWords*2); i++){
                            tempResults[i] = new char[RMAX_LEN];
                    //Copy all entries in results into the new array
                    for(i = 0; i < maxWords; i++){
                            for(j = 0; j < RMAX_LEN; j++){
                                    tempResults[i][j] = results[i][j];
                            }// End of for
                    } //End of for
                    maxWords *= 2; //change maxwords to reflect new buffer size
                    for(i = 0; i < maxWords; i++){ //Free up old reserved space
    			delete [] results[i];
                    delete [] results;
                    results = tempResults; //results now points to new array
            //Then copy in the word
            j = (unsigned int)word.length();
            for(i = 0; i < j; i++){
                    results[numWords][i] = word[i];
            } //End of for
            results[numWords][i] = '\0';
    }// End of saveWord
    And the compiler error
    bash-3.00$ g++ pa1.cpp
    pa1.cpp: In function `void saveWord(std::string&, char (*)[60], int&, int&)':
    pa1.cpp:119: error: cannot convert `char**' to `char (*)[60]' in assignment

    By the way, is my method of creating the new array the most desirable method or is there a better one?

  2. #2
    Join Date
    Dec 2003
    Its usually desirable to use single dimensional and just index it or cast it out as a 2-d. Use of vectors instead is often recommended as well, unless you have a reason not to. the compiler problem can be worked around, however, and your code looks correct except for that one problem.

    results = &tempResults[0]; //I think this will cure it? try it and see. I dont have a compiler at home atm as I reloaded recently.

  3. #3
    Join Date
    Oct 2005
    thanks for your reply. Im also not at a pc with a compiler so i cant test it as of yet but i do have a question

    In a case like char **tempResults arent tempresults and &tempResults[0] the same thing?

  4. #4
    Join Date
    Dec 2003
    Almost. the address is the same but the assignment should see it as ** instead of * to array of [60].

  5. #5
    Join Date
    Oct 2005
    Unfortunately it doesnt seem to be working. I got the exact same error. Any other ideas?

  6. #6
    Join Date
    Dec 2003
    I will sneak a compile at it tomorrow at work. There is a simple way to trick the compiler into seeing them as the same type, but as I always just use single dimensional arrays, I can't think it up atm.

  7. #7
    Join Date
    Dec 2003
    void saveWord(string &word, char **results, int &numWords, int &maxWords)

    does this work for you? It did over here, or seems to with my test code.

Similar Threads

  1. delete 2D array - Created Dynamically
    By PoojaPatel in forum C++
    Replies: 22
    Last Post: 02-17-2008, 11:34 AM
  2. Dynamically setting integer arrays
    By jcweller in forum .NET
    Replies: 1
    Last Post: 08-29-2005, 09:27 PM
  3. Replies: 2
    Last Post: 04-15-2005, 09:06 PM
  4. Accessing jagged array values
    By Jason Salas in forum ASP.NET
    Replies: 2
    Last Post: 04-20-2003, 11:39 PM
  5. Replies: 5
    Last Post: 05-24-2002, 05:10 AM

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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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