简体   繁体   中英

Need help converting this snippet from c to c#

typedef struct {
    int e1;
    int e2;
    int e3;
    int e4;
    int e5;
} abc;


void Hello(abc * a, int index)
{
    int * post = (&(a->e1) + index);

    int i;
    for(i = 0; i<5; i++)
    {
        *(post + i) = i;    
    }
}

The problem I face here is how they able to access the next element in the struct by

*(post + i)

I'm not sure how all these would be done in C# and moreover, I don't want to use unsafe pointers in C#, but something alternate to it.

Thanks!

You should replace the struct with an array of 5 elements.

If you want to, you can wrap the array in a class with five properties.

edit...

When you say 'Wrap,' it generally means to write properties in a class that set or get the value of either a single variable, an array element, or a member of another class whose instance lives inside your class (the usual usage here = 'wrap an object'). Very useful for separating concerns and joining functionality of multiple objects. Technically, all simple properties just 'wrap' their private member variables.

Sample per comment:

class test
{

    int[] e = new int[5];
    public void Hello(int index)
    {
        for (int i = 0; i <= 4; i++) {
            // will always happen if index != 0
            if (i + index > 4) {
                MsgBox("Original code would have overwritten memory. .Net will now blow up.");
            }
            e[i + index] = i;
        }
    }

    public int e1 {
        get { return e[0]; }
        set { e[0] = value; }
    }

    public int e2 {
        get { return e[1]; }
        set { e[1] = value; }
    }

    //' ETC etc etc with e3-e5 ...

}

The problem with the C code is that if index is greater than 0 it runs off the end of the abc struct, thus overwriting random memory. This is exactly why C#, a safer language, does not allow these sorts of things. The way I'd implement your code in C# would be:

struct abc
{
    public int[] e;
}

void Hello(ref abc a, int index)
{
    a.e = new int[5];
    for (int i = 0;  i < 5;  ++i)
        a.e[index + i] = i;
}

Note that if index > 0, you'll get an out of bounds exception instead of possibly silent memory overwriting as you would in the C snippet.

The thinking behind the C codes is an ill fit for C#. The C code is based on the assumption that the fields of the struct will be placed sequentially in memory in the order defined the fields are defined in. The above looks like either homework or a contrived example. Without knowing the real intent it's hard to give a concrete example in C#.

other examples here suggest changing the data structure but if you can't/don't want to do that, you can use reflection combined with an array of objects of the struct type to accomplish the same result as above.

void Hello(abc currentObj){
  var fields = typeof(abc).GetFields();
  for(var i = 0;i<fields.Length;i++){
    fields[i].SetValue(currentObj,i);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM