Question

For example, I have a struct:

struct values {
    int value1;
    int value2;
};

Then I initialize it:

struct values *datas = (struct values *) malloc(size * sizeof(struct values) );

And filled it. So, me need to convert few structs to managed, put it into List, delete one struct and convert it back.

Basically, I have few unmanaged structs and I need to delete one with .net and then delete it from memory.

NOTE: Sorry my bad English!

Was it helpful?

Solution

It's my own solution. For the first I fill the struct

for (int i = 0 ; i < size ; i++ )
        {
            (datas+i)->value1 = i;
            (datas+i)->value2 = i;
        }

After, I create the Class, which save the data from datas

public ref class Values
{
private:
    int _value1;
    int _value2;
public:
    Values();
    property int Value1
    {
        int get();
        void set(int value);
    }
    property int Value2
    {
        int get();
        void set(int value);
    }
};

So, I can fill the List with data from my struct

        List<Values^>^ mValues = gcnew List<Values^>();

        for (int i = 0 ; i < size ; i++ )
        {
            Values^ newValues = gcnew Values();
            newValues->Value1 = (datas+i)->value1;
            newValues->Value2 = (datas+i)->value2;

            mValues->Add(newValues);
        }

After all, I can remove one of item by the mValues->RemoveAt(1);? clear the memory free(datas); and recreate struct with new size

size = mValues->Count;
datas = (struct values *) malloc(size * sizeof(struct values) );

And fill it from List

for (int i = 0; i < size; i++)
        {
            Values^ curValue = mValues[i];
            (datas+i)->value1 = curValue->Value1;
            (datas+i)->value2 = curValue->Value1;
        }

That's all, I hope it can help someone!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top