Thread: Pointers and multi dimensional arrays

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14

    Unhappy Pointers and multi dimensional arrays

    Hi,
    I have troubles understanding the relationship between pointers (to double) and multidimensional arrays (of doubles).

    I have a C++ routine that returns pointers to doubles, and a class object that I'm creating computes multidimensional arrays, because I like to think of the data as matrices.

    Then when the computation is done all I have to do is to copy the array values into the location pointed by the pointer, increment it and so on.

    So this is what I have:
    Code:
    void STDCALL FIESUB ( double *dfddis, double *dfdvel )
    {
    ...
    }
    In this routine I instantiate an object of the class Field:
    Code:
        class msField
        {
        private:
            double m_dfdx[6][6];
            double m_dfdxdot[6][6];
        public:
            void getForceDerivatives(double *fddist, double *fdvel);
        };
    The idea being that in getForceDerivatives I would do something like this:
    Code:
    void msField::getForceDerivatives(double *fddist, double *fdvel)
    {
        // retrieves the force derivatives from the field member datas
        int i, j;
        for ( i = 0; i < 6; i++)
        {
            for ( j = 0; j < 6; j++)
            {
                *fddist++=m_dfdx[i][j];
                *fdvel++=m_dfdxdot[i][j];
            }
        }
    }
    That function is called like this (inside of FIESUB):
    Code:
    rField.getForceDerivatives(dfddis, dfdvel);
    The result?
    Mayhem.
    Obviously I am forgetting something.
    Can anyone explain the relationship between pointers to double and arrays of doubles (multidimensional arrays, that is...).
    Any help much appreciated!!
    And72.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A pointer to a double is just that, a pointer to a double. It can be used as different things, including a pointer to a single double somewhere in memory, or the pointer to a double that is part of a single-dimensional array of doubles.

    In your code, you have fddist and fdvel, which are pointers to double. You use them as if they were pointing to single dimensional arrays of double. You increment them 36 times in your nested for loops, so you are assuming that they each originally pointed to an array that is at least 36 doubles long.

    The fact that you are also using multi-dimensional arrays here should not be a problem. It appears that you are using those correctly, assuming you have initialized them to valid values.

    The potential source of mayhem is if fddist and/or fdvel do not point to an array of at least 36 doubles. If they point to single instances of a double, or if they point to nowherem or if they point to an array whose size is smaller than 36, then you will likely get problems.

    It might help if you explained what you mean by mayhem, and if you post how dfddis and dfdvel are originally created or assigned to, since those are the pointers you are passing to the function.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14

    re: pointers and multidimensional arrays

    Hi and thanks for your answer.
    The problem is that I am writing an extension: in other words I do not control upstream from the routine in question.
    So by mayhem I mean that the main application (in development) is getting a bunch of NAN (not a number?) as a consequence of my assignment.
    Now looking at the code i suspect that I am doing something wrong in the computation, not in the assignment.
    I trust that whoever wrote the main application reserved enough memory for storing 36 values, but I will double check that.
    Lots of blathering on my part, but thanks for double checking my code.
    And72.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you might try using meaningful variable names too! what the hell is dfddis supposed to mean? It looks like your left hand went nuts on the keyboard!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by ChaosEngine
    you might try using meaningful variable names too! what the hell is dfddis supposed to mean? It looks like your left hand went nuts on the keyboard!
    Those variable names kind of make sense to me...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You mean forward_distance / forward_velocity? Although the speed of development is important, typing speed is not really a factor here.

    (One thing I dislike about C is its love for acronyms - I'd take size over strlen every day Is it because C doesn't have namespaces - obfuscating the names makes naming collisions less probable?)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2-d arrays and fscanf
    By drdepoy in forum C Programming
    Replies: 5
    Last Post: 11-03-2005, 07:26 AM
  2. silence warning when assigning pointers
    By eth0 in forum C Programming
    Replies: 5
    Last Post: 10-27-2005, 11:18 AM
  3. A multi part Q
    By pxleyes in forum C Programming
    Replies: 7
    Last Post: 03-19-2004, 10:45 PM
  4. Pointers to multi dimensional arrays.
    By bartybasher in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2003, 02:41 PM
  5. Help with mult-dim arrays and pointers to them
    By skybolt_1 in forum C Programming
    Replies: 11
    Last Post: 05-02-2003, 11:47 AM