Returning two-dimensional array in C++

This is a discussion on Returning two-dimensional array in C++ within the C++ Programming forums, part of the General Programming Boards category; gcc (and g++) doesn't seem to allow functions returning arrays. I believe that it is not possible to return an ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,435
    gcc (and g++) doesn't seem to allow functions returning arrays.
    I believe that it is not possible to return an array in both C and C++.

    Returning a pointer to pointer won't work, since you loose the dimension of the array. You will need to indicate at least the last dimension of the array.
    Yes, so we can return a pointer to the first element of the array. Since I am not sure of the syntax, I shall take the liberty of using a typedef:
    Code:
    class MyClass {
      public:
      typedef int(*ArrayType)[5];
      ArrayType getArray()
      {
        return array;
      }
    
      private:
      int array[5][5];
    };
    So there is no way to do it without vectors?
    See above. The thing is, vectors can make things easier.

    (Hm, maybe I should've posted in the C-forum instead, given the devlopement of this thread. Oh well..)
    This is part of C++'s C heritage, so you will face the same problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    I believe that it is not possible to return an array in both C and C++.
    Yes, I think it relates back to the "you can't assign arrays" that C also has in it's basic setup.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you are sure you want to be able to modify the array from outside the class, I don't see much point in making it private in the first place.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #19
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Quote Originally Posted by anon View Post
    If you are sure you want to be able to modify the array from outside the class, I don't see much point in making it private in the first place.
    I don't really need to modify it. I just want to check the data. So it can be defined as const pointer.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Drogin View Post
    I don't really need to modify it. I just want to check the data. So it can be defined as const pointer.
    Are you sure that you don't want to "check the data" in a member/friend function?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    You still haven't addressed why you are so anti-vector, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    You still haven't addressed why you are so anti-vector, however.
    Have you considered that not everyone should have to motivate their design decisions with you?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    Have you considered that I want to know why the OP is so anti-vector?
    I'm not the only one suggesting the idea to use a vector. Laserlight proposed (or clarified) that they made things easier, so I'd like to know (especially with Java background!) why someone is so anti-vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Have you considered that I want to know why the OP is so anti-vector?
    Start a new thread or send a PM. I too think a vector makes sense in many cases, but there's no need to clutter a thread with multiple posts about it when the OP is obviously going in a different direction.

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    But it's also part of suggesting a solution. Or helping solve the problem or gaining a better understanding of the problem.
    Is the OP afraid of C++ objects? Is the OP afraid of vector? Is it speed concerns? Is it memory concerns?
    And this is only the 2nd. If it gets ignored after the 2nd time, then maybe it's time to start evaluating if it's ignored on purpose.
    Even so, it's also somewhat rude to ignore posts, especially from people trying to help. It's all for the sake of getting a better understanding of the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,635
    My personal choice would be to flatten the 2D array first and return a pointer to that, if some other container wasn't available.

    I believe someone demonstrated this but it bears repeating:
    Code:
    #include <cstddef>
    #include <cstring>
    
    class stupidmatrix
    {
    public:
        stupidmatrix (std::size_t rowheight, std::size_t colwidth):
          _rows(rowheight),
          _cols(colwidth),
          _impl(new int [_rows*_cols])
          //If you had an existing 2D-array, you could just allocate a new pointer like this,
          //copy the data over, and then delete and reassign the old pointer. Try this assignment
          //in a loop:
          //newp[row*_rows+i] = oldp[row][i];
          {
    
          }
    
          ~stupidmatrix ()
          {
              delete[] _impl;
          }
    
          int * get ()
          {
              return _impl;
          }
    private:
        int * _impl;
        const std::size_t _rows;
        const std::size_t _cols;
    };
    Not a complete example at all, but I think you should use or build a matrix class.

  12. #27
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Drogin View Post
    I don't really need to modify it. I just want to check the data. So it can be defined as const pointer.
    Make the check a member function. No need to return anything.

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Not a complete example at all, but I think you should use or build a matrix class.
    I know it's not a complete example, but since so many people miss it I would prefer to have it include a comment about or disabling of copying.

    >> But it's also part of suggesting a solution.
    I don't mind it being suggested once, or being repeated when tacked on to another point that is relevant to the discussion.

    >> And this is only the 2nd.
    You mentioned vectors in five separate posts and added almost nothing else of value. The OP responded that he or she did not want to use vectors and even intimated why. The OP also made it clear that he or she did not think further discussion of vectors was relevant. So, take the hint. It is their loss if a vector would be the best choice but they choose to avoid it. I'm ashamed to be cluttering the thread even further, but I think a public explanation is best here.

  14. #29

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    There's nothing wrong with using arrays, if you don't want vectors. It's definitely possible to return an array, but it looks ugly:

    Code:
    class MyClass
    {
        private:
            /* I assume you want the const-ness of this
                 array to be implementation defined: */
            int array[5][5];
            /* Otherwise, you can of course explicitly
                make it mutable or const, and get rid of
                the unecessary getter function, below: */
    
        public:
            /* Const getter-function: */
            const int (&getArray() const)[5][5] { return array; }
    
            /* Non-const getter: */
            int (&getArray())[5][5] { return array; }
    };
    This is more direct and simple than returning a pointer to a pointer to an integer. You could also make the array dimensions into a macro so you never need to repeat the '[5][5]'.
    Last edited by rudyman; 06-18-2008 at 06:01 PM.

  15. #30
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    int ** getArray()

    this doesn't help you keep track of the array dimensions though if they're ever something other than 5 and 5; you'll need something like:

    unsigned int get1DLength()

    and

    unsigned int get2DLength(int 1dPos)

    this is probably not the best way to do it tho.
    Last edited by m37h0d; 06-18-2008 at 01:31 PM.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. problem returning array from function(among others)
    By Calef13 in forum C++ Programming
    Replies: 30
    Last Post: 10-30-2006, 03:26 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21