pinvoke marshalling of 2d multidimensional array of type double as input and output between c# and c++

后端 未结 1 1522
忘掉有多难
忘掉有多难 2021-01-15 19:37

I have the following c# and c++ pinvoke marshalling of 2d multidimensional array of type double matter I\'m trying to solve.

I\'ve reviewed the following hit to ge

1条回答
  •  太阳男子
    2021-01-15 19:47

    This code:

    *values = new double[*valuesOuterLen, *valuesInnerLen];
    

    does not do what you think it does. (I shortened the variable names because they just complicate things.)

    C++ inherits from C a lack of multi-dimensional arrays. What it does have is arrays of arrays, or arrays of pointers to arrays. In both cases you index these as array[firstIndex][secondIndex]. You can't new an array of pointers like that, you have to write something like:

    double*** values;  // Triple pointer!  Ow!!  *values is a pointer to
                       // (an array of) pointers to (arrays of) doubles.
    *values = new double*[*valuesOuterLen];
    for (size_t i=0; i

    What you have actually invoked is the C++ comma operator, which evaluates and discards the first operand, and then evaluates the second operand. Crank your compiler warnings up; a good compiler will warn that the first operand has no side-effects.

    0 讨论(0)
提交回复
热议问题