0

I'm having some trouble converting the following code from c++ to c# because of pointers.

Basically I have a

STATE** State;
States = new STATE* [max_states];

for (int i=0; i < max_states; i++) {
  States[i] = new STATE(max_symbols);
}

If this was some double array I would say

STATE[][]  States;  
States = new STATE[max_states][];

for (int i = 0; i '<' max_states; i++) {
    States[i] = new STATE[max_symbols];
}

But the problem is the c++ code is not working "as" I expected it to.

States[i] = new STATE(max_symbols);

Has some strange behavior that for example allows

States[cur_state]->set_recur_out(k);

what exactly am I not seeing? This might be a beginner c++ question. Sorry if I don't make any sense at all =)

2 Answers 2

3

it is not a 2d-array, but a 1d-array containing pointers to single elements...
new STATE(max_symbols) constructs a single STATE object, calling the constructor which takes a single argument (in this case max_symbols).

i dont have that much clue of C#, but the following should be the correct representation of the C++ code in C#:

STATE[]  States;  
States = new STATE[max_states];

for (int i = 0; i '<' max_states; i++) {
    States[i] = new STATE(max_symbols);
}
Sign up to request clarification or add additional context in comments.

Comments

1

This is simply an array of pointers. In C# that would be a one-dimensional array:

STATE[]  States = new STATE[max_states];

for (int i = 0; i < max_states; i++) {
    States[i] = new STATE(max_symbols);
}

The “strange behavior” that you are seeing in the C++ is simply the C++ way of accessing a member of a pointer to a type:

Type* x;
x->y();
// is the same as:
(*x).y();
// and corresponds to:
Type z;
z.y();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.