indexing elements of a 2D array by pointer for transpose

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pc_whocares

    indexing elements of a 2D array by pointer for transpose

    My forehead is flat from pounding.

    I am building a DLL in VS2005 C++ for use in another software
    development platform.

    I am required to pass my array data in/out of the function via a
    pointer, in this case, to double.

    The simplest way for me to transpose the array is via addressing its
    individual elements in the conventional form such as

    array2dt[i,j] = array2d[j,i];

    where both variables appear in the call list as

    long redimSTr(..., double *array2d, double *array2dt)

    I realize that the algorithm looks like it will likely only work for a
    square matrix, but since array2dt is coming back completely unchanged,
    I suspect that there is something else fundamentally wrong with my
    logic, specifically, the way to directly address individual elements
    of a multi dimensional array represented by a pointer in a function.

    As you may suspect, please don't assume I'll just catch a hint.
    'splain to me, Lucy!

    tanks

    pc
  • Gianni Mariani

    #2
    Re: indexing elements of a 2D array by pointer for transpose



    pc_whocares wrote:
    My forehead is flat from pounding.
    ....
    As you may suspect, please don't assume I'll just catch a hint.
    'splain to me, Lucy!
    Without more code, no-one will be able to help you.

    Comment

    • pc_whocares

      #3
      Re: indexing elements of a 2D array by pointer for transpose

      On Aug 4, 6:06 pm, pc_whocares <pc_whoca...@ya hoo.comwrote:
      My forehead is flat from pounding.
      >
      I am building a DLL in VS2005 C++ for use in another software
      development platform.
      >
      I am required to pass my array data in/out of the function via a
      pointer, in this case, to double.
      >
      The simplest way for me to transpose the array is via addressing its
      individual elements in the conventional form such as
      >
      array2dt[i,j] = array2d[j,i];
      >
      where both variables appear in the call list as
      >
      long redimSTr(..., double *array2d, double *array2dt)
      >
      I realize that the algorithm looks like it will likely only work for a
      square matrix, but since array2dt is coming back completely unchanged,
      I suspect that there is something else fundamentally wrong with my
      logic, specifically, the way to directly address individual elements
      of a multi dimensional array represented by a pointer in a function.
      >
      As you may suspect, please don't assume I'll just catch a hint.
      'splain to me, Lucy!
      >
      tanks
      >
      pc
      Thanks for your replies. Let me try again.

      I need a way to manipulate a 2D array in a subroutine. What's the
      magic thing that will allow me to go from a pointer and base type to a
      2D array? I am obviously a bare novice here.

      It seems that one cannot dynamically create a 2D array.

      tanks

      pc


      // redim.cpp : Defines the entry point for the DLL application.
      //
      // Use the defined keyword DLLEXPORT in front of C/C++ functions
      // that are meant to be exported for use in calling context
      //
      #include <stdlib.h>
      #include "stdafx.h"

      #ifdef WIN32
      #ifdef __cplusplus
      #define DLLEXPORT extern "C" __declspec(dlle xport)
      #else
      #define DLLEXPORT __declspec(dlle xport)
      #endif
      #else
      #define DLLEXPORT
      #endif

      typedef double* dblArrayPtr;

      DLLEXPORT long redimSTr(long n, long m, long arraySize, double SF,
      double *array, double *array2d)

      {
      long i;
      long j;

      dblArrayPtr tmpArray;
      // both errors next executable line
      // -- apparently can't dynamically create 2D array
      // Error 1 error C2540: non-constant
      // expression as array bound
      // Error 2 error C2440: '=' :
      // cannot convert from 'double (*)[1]' to 'dblArrayPtr'
      tmpArray = new double[n][m];

      // from calling context array2d is m x n
      // next for loop simply copies data from
      // 1D "array" to 2D "array2d" and scales it
      for (i = 0; i < arraySize; i++, array++, array2d++)
      {
      *array2d = *array * SF;
      }

      // intention here is to put scaled data into
      // an array that I can address by row, column
      tmpArray = array2d;

      // perform transpose.. will only work for square matrix,
      // but that's the least of my worries
      for (i = 0; i < n; i++)
      {
      for (j = 0; j < m; j++)
      {
      tmpArray[i,j] = array2d[j,i];
      }
      }

      // put transposed data back so I can access
      // it from calling function
      array = tmpArray;
      // clean up
      delete [] tmpArray;
      // arraySize is unused upon return
      return(arraySiz e);
      }

      Comment

      • red floyd

        #4
        Re: indexing elements of a 2D array by pointer for transpose

        On Aug 5, 10:32 am, pc_whocares <pc_whoca...@ya hoo.comwrote:
        On Aug 4, 6:06 pm, pc_whocares <pc_whoca...@ya hoo.comwrote:
        >
        >
        >
        My forehead is flat from pounding.
        >
        I am building a DLL in VS2005 C++ for use in another software
        development platform.
        >
        I am required to pass my array data in/out of the function via a
        pointer, in this case, to double.
        >
        The simplest way for me to transpose the array is via addressing its
        individual elements in the conventional form such as
        >
            array2dt[i,j] = array2d[j,i];
        >
        where both variables appear in the call list as
        >
           long redimSTr(..., double *array2d, double *array2dt)
        >
        I realize that the algorithm looks like it will likely only work for a
        square matrix, but since array2dt is coming back completely unchanged,
        I suspect that there is something else fundamentally wrong with my
        logic, specifically, the way to directly address individual elements
        of a multi dimensional array represented by a pointer in a function.
        >
        As you may suspect, please don't assume I'll just catch a hint.
        'splain to me, Lucy!
        >
        tanks
        >
        pc
        >
        Thanks for your replies.  Let me try again.
        >
        I need a way to manipulate a 2D array in a subroutine.  What's the
        magic thing that will allow me to go from a pointer and base type to a
        2D array?  I am obviously a bare novice here.
        >
        It seems that one cannot dynamically create a 2D array.
        >
        tanks
        >
        pc
        >
        // redim.cpp : Defines the entry point for the DLL application.
        //
        // Use the defined keyword DLLEXPORT in front of C/C++ functions
        // that are meant to be exported for use in calling context
        //
        #include <stdlib.h>
        #include "stdafx.h"
        >
        #ifdef WIN32
        #ifdef __cplusplus
        #define DLLEXPORT extern "C" __declspec(dlle xport)
        #else
        #define DLLEXPORT __declspec(dlle xport)
        #endif
        #else
        #define DLLEXPORT
        #endif
        >
        typedef double* dblArrayPtr;
        >
        DLLEXPORT long redimSTr(long n, long m, long arraySize, double SF,
        double *array, double *array2d)
        >
        {
                long i;
                long j;
        >
                dblArrayPtr tmpArray;
                // both errors next executable line
                //      -- apparently can't dynamically create 2D array
                // Error        1       error C2540: non-constant
                //      expression as array bound
                // Error        2       error C2440: '=' :
                //      cannot convert from 'double (*)[1]' to 'dblArrayPtr'
                tmpArray = new double[n][m];
        >
                // from calling context array2d is m x n
                // next for loop simply copies data from
                // 1D "array" to 2D "array2d" and scales it
                for (i = 0; i < arraySize; i++, array++, array2d++)
                {
                        *array2d = *array * SF;
                }
        >
                // intention here is to put scaled data into
                // an array that I can address by row, column
                tmpArray = array2d;
        >
                // perform transpose.. will only work for square matrix,
                // but that's the least of my worries
                for (i = 0; i < n; i++)
                {
                        for (j = 0; j < m; j++)
                        {
                                tmpArray[i,j] = array2d[j,i];
        this line does not do what you think it does. It actually does
        tmpArray[(i,j)] = array2d[(i,j)]

                        }
                }
        >
                // put transposed data back so I can access
                //      it from calling function
                array = tmpArray;
                // clean up
                delete [] tmpArray;
                // arraySize is unused upon return
                return(arraySiz e);
        >
        }
        >
        >

        Comment

        • Kevin Frey

          #5
          Re: indexing elements of a 2D array by pointer for transpose

          In C++, for an array having multiple dimensions, two adjacent elements in
          the last dimension of the array are arranged contiguously in memory *, and
          you work back from that.

          * I say contiguously - but this will include allowances for issues such as
          structure padding and alignment etc. The important point is that the
          elements are effectively "next to each other" in memory.

          You *must* know the dimensions of the array.

          So an array like a[5,10] will have p0=a[0,0], p1=a[0,1], p2=a[0,2], ...
          p10=a[1,0] etc, where p0 is a memory location, and p1 is the next memory
          location etc. (Next contiguous memory location based on the SIZE of the
          elements, that is).

          You can write a simple class to handle the access to a multi-dimensional
          array, so it does the conversions for you.

          For example (I am writing this off the top of my head, so it may not compile
          cleanly):

          template< class T >
          class TwoDimArray
          {
          public:
          TwoDimArray( T* _Buffer, int _FirstDimension , int _SecondDimensio n )
          {
          m_Buffer = _Buffer;
          m_FirstDim = _FirstDimension ;
          m_SecondDim = _SecondDimensio n;
          }

          T& Item( int d1, int d2 )
          {
          // you could do assertions etc here to check that d1, d2 are
          "in-range"

          int sequentalPositi on = ( d1 * m_SecondDim ) + d2;
          return m_Buffer[ sequentialPosit ion ];
          }

          T* m_Buffer;
          int m_FirstDim;
          int m_SecondDim;
          };

          int main( )
          {
          double* buffer = new buffer [ 100 ];

          TwoDimArray arr( buffer, 10, 10 );

          arr.Item( 0, 0 ) = 100.0;
          arr.Item( 1, 7 ) = 200.0;
          }


          Comment

          Working...