locked
Adding dimensions to multi-dimension array RRS feed

  • Question

  • Is it possible to add dimensions to an existing multi-dimensioned array?

    after the array was dimensioned;

             std::string **griddta;  
             griddta = new std::string*[columns];

             for(x=0;x<columns;x++)
             {
               griddta[x] = new std::string[rows];
             }

    then I want to add more rows...

    Friday, October 9, 2015 6:28 PM

Answers

  • Thanks for the example, Wayne... but it doesn't even compile for me in Visual Studio 2013

    Error    1    error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup  

    You appear to have used the Win32 Project template.
    You should have used the Win32 Console Application template.

    Or go into the existing Project Properties and change the Linker setting for
    "System->Subsystem" to "Console" and rebuild.

    - Wayne

    • Marked as answer by Shu 2017 Monday, October 19, 2015 10:05 AM
    • Unmarked as answer by TallGuy63 Monday, October 19, 2015 1:58 PM
    • Marked as answer by TallGuy63 Monday, October 19, 2015 1:58 PM
    Sunday, October 11, 2015 12:29 AM
  • Is it possible to add dimensions to an existing multi-dimensioned array?

    after the array was dimensioned;

             std::string **griddta;  
             griddta = new std::string*[columns];

             for(x=0;x<columns;x++)
             {
               griddta[x] = new std::string[rows];
             }

    then I want to add more rows...

    What happens when you run this example?

    #include "stdafx.h"
    #include <string>
    #include <iostream>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int columns = 3, rows = 2, x;
    
        std::string **griddta;  
        griddta = new std::string*[columns];
    
        for(x=0;x<columns;x++)
            {
            griddta[x] = new std::string[rows];
            for(int z = 0; z < rows; ++z)
                {
                griddta[x][z] = "First allocation.";
                }
            }
    
        for(x=0;x<columns;x++)
            {
            for(int z = 0; z < rows; ++z)
                {
                std::cout << griddta[x][z] << '\n';
                }
            }
    
        rows++;
    
        for(x=0;x<columns;x++)
            {
            delete[] griddta[x];
            griddta[x] = new std::string[rows];
            for(int z = 0; z < rows; ++z)
                {
                griddta[x][z] = "Second allocation.";
                }
            }
    
        for(x=0;x<columns;x++)
            {
            for(int z = 0; z < rows; ++z)
                {
                std::cout << griddta[x][z] << '\n';
                }
            }
    
        return 0;
    }
    

    Expected output:

    First allocation.
    First allocation.
    First allocation.
    First allocation.
    First allocation.
    First allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.

    - Wayne

    • Proposed as answer by Shu 2017 Monday, October 19, 2015 10:05 AM
    • Marked as answer by Shu 2017 Monday, October 19, 2015 10:05 AM
    Saturday, October 10, 2015 10:30 PM

All replies

  • Is it possible to add dimensions to an existing multi-dimensioned array?

    after the array was dimensioned;

             std::string **griddta;  
             griddta = new std::string*[columns];

             for(x=0;x<columns;x++)
             {
               griddta[x] = new std::string[rows];
             }

    then I want to add more rows...

    You would have to initialize a new array of std::string** with the desired length, copy the array of pointers, then delete griddta. Assign the griddta to it. Then add the additional rows.

    Generally, multi dimension arrays are best handled in a class using a single vector and indexing into the desired position. Multiple levels of indirection can be slow due to memory access. Boost.org has a good multi dimension class. Also these approaches automate memory cleanup which is a PITA.

    Friday, October 9, 2015 6:50 PM
  • You can use collection classes for it vector or List etc.

    Thanks


    Rupesh Shukla

    Friday, October 9, 2015 6:57 PM

  • then I want to add more rows...

    Just a nit, but adding rows is not the same as adding dimensions.

    Saturday, October 10, 2015 1:36 AM
  • Is it possible to add dimensions to an existing multi-dimensioned array?

    after the array was dimensioned;

             std::string **griddta;  
             griddta = new std::string*[columns];

             for(x=0;x<columns;x++)
             {
               griddta[x] = new std::string[rows];
             }

    then I want to add more rows...

    You have set up your array so that griddta[i][j] is row j in column i, rather than the more conventional column j in row i. Is there a particular reason you did this?

    Anyway, the way you set it up makes it easier to add more rows than more columns, because you only have to reassign griddta[i] for each column i.


    David Wilkinson | Visual C++ MVP

    Saturday, October 10, 2015 8:20 AM
  • I can't seem to redimension it...

    If I do this I get an error:

         rows++;

         for(x=0;x<columns;x++)
         {
           griddta[x] = new std::string[rows];
         }

    because the array is already dimensioned, so it doesn't like the fact that I'm only giving one dimension to griddta[][]

    Saturday, October 10, 2015 9:04 PM
  • If I do this I get an error:

         rows++;

         for(x=0;x<columns;x++)
         {
           griddta[x] = new std::string[rows];
         }

    *What* error do you get? *Exactly*, not a paraphrase.

    Note that just assigning a new value to each pointer will result in a memory leak.
    You should delete[] the old allocation first.

    - Wayne

    Saturday, October 10, 2015 10:05 PM
  • Is it possible to add dimensions to an existing multi-dimensioned array?

    after the array was dimensioned;

             std::string **griddta;  
             griddta = new std::string*[columns];

             for(x=0;x<columns;x++)
             {
               griddta[x] = new std::string[rows];
             }

    then I want to add more rows...

    What happens when you run this example?

    #include "stdafx.h"
    #include <string>
    #include <iostream>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int columns = 3, rows = 2, x;
    
        std::string **griddta;  
        griddta = new std::string*[columns];
    
        for(x=0;x<columns;x++)
            {
            griddta[x] = new std::string[rows];
            for(int z = 0; z < rows; ++z)
                {
                griddta[x][z] = "First allocation.";
                }
            }
    
        for(x=0;x<columns;x++)
            {
            for(int z = 0; z < rows; ++z)
                {
                std::cout << griddta[x][z] << '\n';
                }
            }
    
        rows++;
    
        for(x=0;x<columns;x++)
            {
            delete[] griddta[x];
            griddta[x] = new std::string[rows];
            for(int z = 0; z < rows; ++z)
                {
                griddta[x][z] = "Second allocation.";
                }
            }
    
        for(x=0;x<columns;x++)
            {
            for(int z = 0; z < rows; ++z)
                {
                std::cout << griddta[x][z] << '\n';
                }
            }
    
        return 0;
    }
    

    Expected output:

    First allocation.
    First allocation.
    First allocation.
    First allocation.
    First allocation.
    First allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.
    Second allocation.

    - Wayne

    • Proposed as answer by Shu 2017 Monday, October 19, 2015 10:05 AM
    • Marked as answer by Shu 2017 Monday, October 19, 2015 10:05 AM
    Saturday, October 10, 2015 10:30 PM
  • Thanks for the example, Wayne... but it doesn't even compile for me in Visual Studio 2013

    Error    1    error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup    D:\Wendes\Test\Test\MSVCRTD.lib(crtexew.obj)
    Error    2    error LNK1120: 1 unresolved externals    D:\Wendes\Test\Debug\Test.exe

    Saturday, October 10, 2015 11:56 PM
  • Thanks for the example, Wayne... but it doesn't even compile for me in Visual Studio 2013

    Error    1    error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup  

    You appear to have used the Win32 Project template.
    You should have used the Win32 Console Application template.

    Or go into the existing Project Properties and change the Linker setting for
    "System->Subsystem" to "Console" and rebuild.

    - Wayne

    • Marked as answer by Shu 2017 Monday, October 19, 2015 10:05 AM
    • Unmarked as answer by TallGuy63 Monday, October 19, 2015 1:58 PM
    • Marked as answer by TallGuy63 Monday, October 19, 2015 1:58 PM
    Sunday, October 11, 2015 12:29 AM