CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    How to pass a N-dimensional array to a function?

    If a function takes N-dimensional array as a parameter, what is generic way to do that? I couldn't figure out a nice way to do that. Any comments are welcome! Thanks.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to pass a N-dimensional array to a function?

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2013
    Posts
    576

    Re: How to pass a N-dimensional array to a function?

    Quote Originally Posted by LarryChen View Post
    Any comments are welcome
    If you want generiticity there's a Boost library class called MultiArray.

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to pass a N-dimensional array to a function?

    Quote Originally Posted by razzle View Post
    If you want generiticity there's a Boost library class called MultiArray.
    We don't use Boost library. I want to implement one on my own. Thanks.

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to pass a N-dimensional array to a function?

    The links you gave are all about two-dimensional array. I am talking about N-dimensional array. Here N could be any number. Any idea? Thanks.

  6. #6
    Join Date
    Jul 2013
    Posts
    576

    Re: How to pass a N-dimensional array to a function?

    Quote Originally Posted by LarryChen View Post
    I am talking about N-dimensional array. Here N could be any number. Any idea?
    Yes, only 1-dimensional programmers use N-dimensional arrays.

    In C++ there's almost always a better data structure than an N-dimensional array (where N is higher than 2).

    I suggest you start learning about the alternatives. Use Boost.MultiArray for cases where there really is no alternative and you'll soon realize you never need to. It's an emergency exit you never have to use.
    Last edited by razzle; September 7th, 2014 at 05:12 AM.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to pass a N-dimensional array to a function?

    Quote Originally Posted by LarryChen View Post
    If a function takes N-dimensional array as a parameter, what is generic way to do that? I couldn't figure out a nice way to do that. Any comments are welcome! Thanks.
    If the dimensions and size are known at compile time then it's trivial. If the dimensions/size are run-time related then one way that I've used in the past (which may not be the best way ) is to have the N-dimensional array as contiguous memory and pass memory pointer, the number of dimensions and the size of each dimension as variable number of parameters to the function. Or you could have a vector with the size of each dimension and pass the memory pointer and the vector. Or have a struct that contains the memory pointer and a vector for the dimension sizes (you can get the number of dimensions from the number of elements in the vector) and pass the struct as reference to the function. If using a struct then the operator() could be overloaded to allow array access with a variable number of parameters and the constructor used to allocate the memory and create the vector of dimension sizes. A skeleton struct could be something like
    Code:
    template <typename T>
    struct myarray {
    	void *arrptr;
    
    	vector <int> dimens;
    
    	myarray(int nodim, ...)
    	{
    		//...
    	}
    
    	T& operator()(int first, ...)
    	{
    		//...
    	}
    
    	~myarray()
    	{
    		//...
    	}
    };
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Mar 2006
    Posts
    151

    Re: How to pass a N-dimensional array to a function?

    Are you just looking for the basic but non-obvious syntax?

    Code:
    // The crucial thing to remember here is to read non-trivial pointer declarations backward
    // but with grouping by parentheses.  So, "pArray is a constant pointer to an aray of
    // 10 x 8 x 3 constant integers".
    void MyFunction(int const (* const pArray)[10][8][3], int nRows)
    {
        for (int i = 0; i < nRows; ++i)
        {
            int x = pArray[i][9][7][2];
        }
    }
    
    void MyOtherFunction(void)
    {
        // Of course in a real program you wouldn't use the hard-coded constants and
        // most people would probably want a typedef.
        int (* pArrayOnHeap)[10][8][3] = nullptr;
        // All but the first dimension must match the declaration.
        pArrayOnHeap = new (int[2][10][8][3]); // Should use a smart pointer.
        MyFunction(pArrayOnHeap, 2);
    
        int arrayOnStack[4][10][8][3];
        // _countof() will only count the first dimension
        MyFunction(arrayOnStack, _countof(arrayOnStack));
    
        delete [] pArrayOnHeap;
    }
    Hope this helps,
    GeoRanger

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured