Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Error using an array of structs that contain an array

Error using an array of structs that contain an array

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bruce Coward
    wrote on last edited by
    #1

    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

    R L 2 Replies Last reply
    0
    • B Bruce Coward

      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

      R Offline
      R Offline
      riced
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • R riced

        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

        B Offline
        B Offline
        Bruce Coward
        wrote on last edited by
        #3

        AIUI it has to be fixed to allocate the 8 bytes and because of this it has to be declared unsafe. Bruce :confused:

        R 2 Replies Last reply
        0
        • B Bruce Coward

          AIUI it has to be fixed to allocate the 8 bytes and because of this it has to be declared unsafe. Bruce :confused:

          R Offline
          R Offline
          riced
          wrote on last edited by
          #4

          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

          B 1 Reply Last reply
          0
          • B Bruce Coward

            AIUI it has to be fixed to allocate the 8 bytes and because of this it has to be declared unsafe. Bruce :confused:

            R Offline
            R Offline
            riced
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • B Bruce Coward

              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

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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

              B 1 Reply Last reply
              0
              • L Luc Pattyn

                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

                B Offline
                B Offline
                Bruce Coward
                wrote on last edited by
                #7

                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

                L 1 Reply Last reply
                0
                • R riced

                  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

                  B Offline
                  B Offline
                  Bruce Coward
                  wrote on last edited by
                  #8

                  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

                  R 1 Reply Last reply
                  0
                  • B Bruce Coward

                    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

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • B Bruce Coward

                      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

                      R Offline
                      R Offline
                      riced
                      wrote on last edited by
                      #10

                      Glad to be of help Funnily, my other post came because I thought in C not c# :)

                      Regards David R

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups