Thread: Returning two-dimensional array in C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A vector of vectors may have too much overhead to be a replacement for small multidimensional stack array. (I believe there are cases where it is justified not to want dynamic allocations.)

    By the way, do you want to return a copy of the array?

    In that case, it might be meaningful to use an out parameter (the contents are copied to a suitable array provided by the caller).

    If you don't want a copy, then you might try returning a pointer to pointer (int**).

    -----
    Best advice depends on what exactly you are doing this for...
    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).

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    A vector of vectors may have too much overhead to be a replacement for small multidimensional stack array. (I believe there are cases where it is justified not to want dynamic allocations.)

    By the way, do you want to return a copy of the array?

    In that case, it might be meaningful to use an out parameter (the contents are copied to a suitable array provided by the caller).

    If you don't want a copy, then you might try returning a pointer to pointer (int**).

    -----
    Best advice depends on what exactly you are doing this for...
    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.

    gcc (and g++) doesn't seem to allow functions returning arrays.

    --
    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. #3
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    I can give you a very simple Java-verison of what I want.
    I will comment it well, so non-java people can read it.

    So, I would like a C++ verison of this java-code:
    Code:
    class MyClass {
        private int[][] iPointer = new int[5][5]; // This makes a new array of int, and let iPointer point to it
    
        // This is the get-function. It returns a pointer to the array
        public int[][] getArray() {
            return iPointer;
        }
    }
    
    
    class MainClass {
        public static void main(String[] args) { // This is the java int main()
            MyClass myclass = new MyClass(); // Run the above class.
            int[][] anotherPointer; // Decleare a pointer that should be pointing to a 2-dimensional int array
            anotherPointer = myclass.getArray(); // Set the pointer to point to the array returned
    
    
            // Then, I want to be able to do this, on the array returned.
            // (This pointer points to the iPointer member in myclass)
            anotherPointer[0][0]=5;
            anotherPointer[0][1]=3;
        }
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should be able to return a pointer to an array in a class. Let me work on it for few minutes.

    Not very pretty:
    Code:
    #include <iostream>
    
    typedef int myarray[5][5];
    
    class myclass
    {
        myarray arr;
    
    public:
        myclass() {
    	for(int i = 0; i < 5; i++)
    	{
    	    for(int j = 0; j < 5; j++)
    		arr[j][i] = i * j;
    	}
        }
        friend std::ostream &operator<<(std::ostream &os, const myclass &c);
        myarray * getArray()
    	{
    	    return &arr;
    	}
    
    
    };
    
    std::ostream &operator<<(std::ostream &os, const myclass &c) 
    {
        for(int i = 0; i < 5; i++)
        {
    	for(int j = 0; j < 5; j++)
    	    os << c.arr[j][i] << " ";
    	
    	os << std::endl;
        }
        
        return os;
    }
    
    
    
    int main()
    {
        myclass c;
        int (*p)[5][5];
       
        std::cout << c;
      
        p = c.getArray();
        for(int i = 0; i < 5; i++)
        {
    	for(int j = 0; j < 5; j++)
    	    std::cout << (*p)[j][i] << " ";
    	std::cout << std::endl;
        }
        return 0;
    
    }
    --
    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.

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, 03:56 AM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. problem returning array from function(among others)
    By Calef13 in forum C++ Programming
    Replies: 30
    Last Post: 10-30-2006, 04: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, 12:48 PM