Menu

Dynamic 2D array: how???

2002-12-06
2012-09-26
  • Nobody/Anonymous

    There's no one home in the users forum, so I'll give it a try here:

    Suppose I want to put a bitmap in a 2D array in memory. I determine the values for width and height in runtime (int w, h; ) and create the array..

    int (*bitmap)[h];
    bitmap= new int[w][h];

    then I can, for instance, get R, G or B values and put them in the array.

    This works fine With GCC, but Borland 5.5 gives an error: it refuses the h. If I put a number there, like

    int (*bitmap)[123];
    bitmap= new int[w][123];

    BCC compiles without objections. But since the dimensions of the bitmap are determined in runtime that is not an option.

    What's going on here? Is my code ok or isn't it?

    Johan

     
    • Matt Fiedler

      Matt Fiedler - 2002-12-06

      GCC actually allows some dynamic allocation of built-in arrays, which is, of course, non-standard behavior and something you should _never_ rely upon.  BCC, on the other hand, doesn't allow this (in conformance with the standard).

      If you're looking for a good, portable implementation of multi-dimensional arrays, try the MultiArray library from www.boost.org.  This will provide a good interface for multi-dimensional arrays and will allow you to hide the memory management.

      Matt

       
    • Nobody/Anonymous

      Isn't there, he asked in total ignorance, some useful stuff in this area in the STL?

      Wayne (the dumb)

       
    • Curtis Sutter

      Curtis Sutter - 2002-12-06

      how about this?

      // My code
      int **bitmap = new int* [w];
      for(int count = 0; count < h; count++)
        bitmap[count] = new int[h];
      //End of my code

      Curtis

       
    • Nobody/Anonymous

      That is the notation, the ** notation that I have seen more often.  See it seldom enough to have it freakin' confuse me every time.

      In my work, most of the time its faster to work with 1D arrays, even with the overhead of the calculations like:

      index = column + (row - 1) * number_columns;

      Can't always do that though.

      ;-)

      Wayne

       
    • Nobody/Anonymous

      Some of the functionality he is looking for can be obtained from the <valarray> header.  Look at the link below for a reference.  However, that's the best the Standard Library gets. 

      http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/doc/node44.html

      The multidimensional array class is probably easier to use and provides semantics closer to what he's trying to do.

       
    • Nobody/Anonymous

      Some of the functionality he is looking for can be obtained from the <valarray> header.  Look at the link below for a reference.  However, that's the best the Standard Library gets. 

      http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/doc/node44.html

      The multidimensional array class is probably easier to use and provides semantics closer to what he's trying to do.

       
    • Nobody/Anonymous

      Cool, thanks for helping my pathetic memory.

      Wayne

       
    • Nobody/Anonymous

      Thanks!
      I'll give Curtis' code a try. I have no idea what is written there (damn pointers), but if it works I won't complain.
      I will check out Boost as well. I'm rather new to c++ and exected it to be a lot like Java... Not always, apparently.

      Johan

       
    • Nobody/Anonymous

      Just for sick grins, I will point out that C# may seem more like Java than C++.

      :-)

      Wayne

       
    • Curtis Sutter

      Curtis Sutter - 2002-12-07

      If you need help with my code, you could upload it to my ftp at ftp://curtis.servebeer.com/ in the uploads dir.  I can fix it there, then you can get it back.

      Curtis

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.