CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.

    Reply
     
    Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 3.00 average. Display Modes
      #1    
    Old November 7th, 2009, 03:35 AM
    Kularic Kularic is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    Kularic is an unknown quantity at this point (<10)
    Unhappy Multi-dimensional arrays on the heap

    Why does this work:
    char * map = new map[x]
    but this doesnt:
    char map = new map[x][y]
    ???
    Is there a way to create multi-dimensional arrays on the heap??
    Reply With Quote
      #2    
    Old November 7th, 2009, 03:36 AM
    Kularic Kularic is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    Kularic is an unknown quantity at this point (<10)
    Re: Multi-dimensional arrays on the heap

    4th line should be:
    char * map = new map[x][y]
    Reply With Quote
      #3    
    Old November 7th, 2009, 03:48 AM
    Kularic Kularic is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    Kularic is an unknown quantity at this point (<10)
    Re: Multi-dimensional arrays on the heap

    Exact error:
    cannot convert `char (*)[6]' to `char*' in initialization
    Reply With Quote
      #4    
    Old November 7th, 2009, 05:36 AM
    Regel Regel is offline
    Junior Member
     
    Join Date: Aug 2009
    Posts: 21
    Regel is an unknown quantity at this point (<10)
    Re: Multi-dimensional arrays on the heap

    Code:
    char** map = new char*[x]; // x rows
    for(int i=0; i < x; i++)
     map[i] = new char[y]; // each row points to a different 1d array.
    Reply With Quote
      #5    
    Old November 7th, 2009, 06:00 AM
    D_Drmmr's Avatar
    D_Drmmr D_Drmmr is offline
    Member
     
    Join Date: Jul 2005
    Location: Netherlands
    Posts: 369
    D_Drmmr has a spectacular aura about (150+)D_Drmmr has a spectacular aura about (150+)
    Re: Multi-dimensional arrays on the heap

    Quote:
    Originally Posted by Kularic View Post
    Is there a way to create multi-dimensional arrays on the heap??
    Instead of using arrays, you should be using vector's. They shield you from the maintenance nightmares of manual memory allocation and provide more functionality than dynamic arrays.
    You can use a multi-dimensional vector like this:
    Code:
    #include <vector>
    int main()
    {
        // create a matrix of 4 x 3 filled with 0
        std::vector<std::vector<int> > matrix(4, std::vector<int>(3, 0));
        matrix[0][0] = 1;
    }
    __________________
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
    Reply With Quote
      #6    
    Old November 7th, 2009, 09:21 AM
    Paul McKenzie Paul McKenzie is offline
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,404
    Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)
    Re: Multi-dimensional arrays on the heap

    Quote:
    Originally Posted by Kularic View Post
    Is there a way to create multi-dimensional arrays on the heap??
    Why not do a search on CodeGuru? This question is one of the most asked and answered questions here. There are at least 100 threads on this topic, both with examples in C and C++, plus if you search the C++ FAQs here, you see an example.

    Regards,

    Paul McKenzie
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:31 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009