Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass an array of double with a pointer as argument

I want to fill an array of doubles from a pointer and then pass the entire array to a C# function.

I declare and initialize the array in the main script as:

double values[3];
for (uint32_t index = 0; index < 3; index++)
{
    std::cout << "[EXE] Set the value for ";
    std::cin >> values[index];
}

setValuesFcn(values); // Call the function passing the pointer

I pass the pointer because on my header I declare the function as bool setValuesFcn(double* values);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The implementation of the function (here is where I have the problem) is:

bool setValuesFcn(double *values)
{
    array<double>^ setValues;

    std::cout << "[DLL] Entering into setMultipleSignals()... " << std::endl;

    for (uint32_t index = 0 ; index < 3; index++)
    {
        setValues[index] = values[index];   // ERROR HERE!
        std::cout << "[DLL] Value[" << index << "]: " << setValues[index] << std::endl;
    }
    if (!veristand_wrapper_cs::VeriStand_dll::SetMultipleSignals(setValues))
        return true;
    else
        return false;
}

I receive an error of System.NullReferenceException that says: Object reference not set to an instance of an object.

I have tried to only print the values with std::cout << "[DLL] Value[" << index << "]: " << values[index] << std::endl; and it works, so my function is able to know the data with a pointer as argument.

Is it possible that the error is due to trying to assign the values ​​of double[] to one of array^? In this case… Is there any solution?

PD: I need that the array that I am trying to fill is of type array<double>^ because the function SetMultipleSignals is allocated in a C# DLL that I have implemented and it expects to get this type of data. The declaration of this function is public static bool SetMultipleSignals(double[] values)

>Solution :

You never instantiated setValues.

values is a stack-allocated array, but setValues is a ^-pointer, which is a pointer to memory allocated and managed by the runtime. Except, you never allocated that memory and the pointer is null, so trying to dereference it gives you a NullReferenceException.

You need:

array<double>^ setValues = gcnew array<double>(3);

See How to: Use Arrays in C++/CLI.

One big clue is that you got an exception from the CLR, which means the error was accessing a managed object, and values is unmanaged, so the error can’t be with accessing values. Another clue is that you never specified the capacity for setValues anywhere

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading