Error using an array of structs that contain an array
-
I am trying to create an array of structs that contain an array using the following code which generates the attached error:
// Structure of address message public struct ADRMSG { public int ulPGN; public byte PGNspec; public byte SourceAddress; unsafe public fixed byte RxData\[8\]; } // Create array of 10 address messages ADRMSG\[\] arAdrMsg = new ADRMSG\[10\]; // Create message indexes private static int AdrMsgFront = 1; private static int AdrMsgRear = 0; unsafe public void fred() { arAdrMsg\[3\].ulPGN = 1; arAdrMsg\[0\].PGNspec = 0XFF; arAdrMsg\[0\].RxData\[0\] = 0x00; } // Error: You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement.
I have tried every addition of fixed I can think of but cant avoid this error. Any suggestions please? Cheers, Bruce
-
I am trying to create an array of structs that contain an array using the following code which generates the attached error:
// Structure of address message public struct ADRMSG { public int ulPGN; public byte PGNspec; public byte SourceAddress; unsafe public fixed byte RxData\[8\]; } // Create array of 10 address messages ADRMSG\[\] arAdrMsg = new ADRMSG\[10\]; // Create message indexes private static int AdrMsgFront = 1; private static int AdrMsgRear = 0; unsafe public void fred() { arAdrMsg\[3\].ulPGN = 1; arAdrMsg\[0\].PGNspec = 0XFF; arAdrMsg\[0\].RxData\[0\] = 0x00; } // Error: You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement.
I have tried every addition of fixed I can think of but cant avoid this error. Any suggestions please? Cheers, Bruce
-
Why does RxData[] have to be declared as unsafe and fixed? From what I understand unsafe and fixed are used when dealing with pointers (i.e. C/C++ like pointers). Might be worth checking usage.
Regards David R
AIUI it has to be fixed to allocate the 8 bytes and because of this it has to be declared unsafe. Bruce :confused:
-
AIUI it has to be fixed to allocate the 8 bytes and because of this it has to be declared unsafe. Bruce :confused:
I've had a play with this and hit the same problem. I don't know enough about how C# gets addresses to see an obvious solution. However, this does compile:
class ADRMSG { public int ulPGN; public byte PGNspec; public byte SourceAddress; unsafe public byte\[\] RxData = new byte \[8\]; } // Create array of 10 address messages ADRMSG\[\] arAdrMsg = new ADRMSG\[10\]; // Create message indexes private static int AdrMsgFront = 1; private static int AdrMsgRear = 0; unsafe public void fred() { arAdrMsg\[3\].ulPGN = 1; arAdrMsg\[0\].PGNspec = 0XFF; arAdrMsg\[0\].RxData\[0\] = 0x00; }
I'm not sure it will give what you want if the address of the RxData array has to be fixed. If I think of anything before I go to bed I'll let you know.
Regards David R
-
AIUI it has to be fixed to allocate the 8 bytes and because of this it has to be declared unsafe. Bruce :confused:
Had a think, and came up with this:
unsafe public void fred() { arAdrMsg\[3\].ulPGN = 1; arAdrMsg\[0\].PGNspec = 0XFF; //arAdrMsg\[0\].RxData\[0\] = 0x00; fixed (ADRMSG\* p = &arAdrMsg\[0\]) { p->RxData\[0\] = (byte)0x00; } }
Again, it compiles but I don't know if it does what you want.
Regards David R
-
I am trying to create an array of structs that contain an array using the following code which generates the attached error:
// Structure of address message public struct ADRMSG { public int ulPGN; public byte PGNspec; public byte SourceAddress; unsafe public fixed byte RxData\[8\]; } // Create array of 10 address messages ADRMSG\[\] arAdrMsg = new ADRMSG\[10\]; // Create message indexes private static int AdrMsgFront = 1; private static int AdrMsgRear = 0; unsafe public void fred() { arAdrMsg\[3\].ulPGN = 1; arAdrMsg\[0\].PGNspec = 0XFF; arAdrMsg\[0\].RxData\[0\] = 0x00; } // Error: You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement.
I have tried every addition of fixed I can think of but cant avoid this error. Any suggestions please? Cheers, Bruce
Hi Bruce, I don't know what the purpose of your question is, anyway fixing a member of a struct, while the struct could be anywhere doesn't make much sense: the offset of the member within the struct will be determined at compile-time, it will not change at run-time; and the location of the struct may or may not change, depending on where it lives: as a local variable, it sits on the stack and can't change; as a member of a reference type, it moves together with that reference type. So what are you doing? is there any Win32 P/Invoke involved? or a fixed file format? or what? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:30 AM
-
Hi Bruce, I don't know what the purpose of your question is, anyway fixing a member of a struct, while the struct could be anywhere doesn't make much sense: the offset of the member within the struct will be determined at compile-time, it will not change at run-time; and the location of the struct may or may not change, depending on where it lives: as a local variable, it sits on the stack and can't change; as a member of a reference type, it moves together with that reference type. So what are you doing? is there any Win32 P/Invoke involved? or a fixed file format? or what? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:30 AM
Hi Luc, Thanks for your response. We have no requirement to "fix" any storage location but when incorporating the array within the struct in order to identify there are 8 bytes within the array the compiler wanted it to be "fixed". We just need a circular queued array of strucures that can be acted upon within this class and also added to from an external class. Cheers, Bruce
-
I've had a play with this and hit the same problem. I don't know enough about how C# gets addresses to see an obvious solution. However, this does compile:
class ADRMSG { public int ulPGN; public byte PGNspec; public byte SourceAddress; unsafe public byte\[\] RxData = new byte \[8\]; } // Create array of 10 address messages ADRMSG\[\] arAdrMsg = new ADRMSG\[10\]; // Create message indexes private static int AdrMsgFront = 1; private static int AdrMsgRear = 0; unsafe public void fred() { arAdrMsg\[3\].ulPGN = 1; arAdrMsg\[0\].PGNspec = 0XFF; arAdrMsg\[0\].RxData\[0\] = 0x00; }
I'm not sure it will give what you want if the address of the RxData array has to be fixed. If I think of anything before I go to bed I'll let you know.
Regards David R
Thanks David, I am still thinking C rather than C# ! Setting it up as a class rather than a struct looks like it will fix my problems. Cheers, Bruce
-
Hi Luc, Thanks for your response. We have no requirement to "fix" any storage location but when incorporating the array within the struct in order to identify there are 8 bytes within the array the compiler wanted it to be "fixed". We just need a circular queued array of strucures that can be acted upon within this class and also added to from an external class. Cheers, Bruce
Hi Bruce, if your struct is used in managed code only, and does not have to exactly match either a file structure or some unmanaged struct (as in Win32 API), then I suggest: - you remove all the unsafe and fixed stuff; - you use a regular array in that struct; - optionally you add a constructor to your struct, which creates the array with 8 elements. So it would look along these lines:
struct aha {
public int someInt;
public int[] theArray;public aha() { theArray=new int\[8\]; }
}
:)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:30 AM
-
Thanks David, I am still thinking C rather than C# ! Setting it up as a class rather than a struct looks like it will fix my problems. Cheers, Bruce