Analysis on C # reference type array

Source: Internet
Author: User

In C/C ++ code, a large number of structures are mixed with common types and arrays. For example, the IMAGE_OPTIONAL_HEADER structure that defines the PE File Header structure is defined as follows:

 
 
  1. typedef struct _IMAGE_DATA_DIRECTORY {  
  2. DWORD VirtualAddress;  
  3. DWORD Size;  
  4. }  
  5. IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;  
  6. #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16  
  7. typedef struct _IMAGE_OPTIONAL_HEADER {  
  8. WORD Magic;  
  9. DWORD NumberOfRvaAndSizes;  
  10. IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];  
  11. }  

In C/C ++, it is completely correct to use arrays in the structure because these arrays will serve as part of the entire structure and directly access the memory block where the structure is located when operating on the structure. However, in C #, this type of language cannot be used directly, because arrays exist as a special C # reference type array, such as definition:

 
 
  1. public struct IMAGE_DATA_DIRECTORY  
  2. {  
  3. public uint VirtualAddress;  
  4. public uint Size;  
  5. }  
  6. public struct IMAGE_OPTIONAL_HEADER  
  7. {  
  8. public const int IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;  
  9. public ushort Magic;  
  10. public uint NumberOfRvaAndSizes;  
  11. public IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];  
  12. }  

If you use the C # reference-type array like definition syntax, as shown in figure

 
 
  1. public struct IMAGE_OPTIONAL_HEADER  
  2. {  
  3. public const int IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;  
  4. public ushort Magic;  
  5. public uint NumberOfRvaAndSizes;  
  6. public IMAGE_DATA_DIRECTORY[] DataDirectory = 
    new IMAGE_DATA_DIRECTORY[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];  
  7. }  

The initialization of the reference type is not allowed in the structure, which is different from the initialization of the class. In this way, the array initialization can only be placed in the constructor, and the structure cannot have any default constructor parameters, which is really troublesome.

 
 
  1. public struct IMAGE_OPTIONAL_HEADER  
  2. {  
  3. public const int IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;  
  4.  
  5. public ushort Magic;  
  6.  
  7. public uint NumberOfRvaAndSizes;  
  8.  
  9. public IMAGE_DATA_DIRECTORY[] DataDirectory;  
  10.  
  11. public IMAGE_OPTIONAL_HEADER(IntPtr ptr)  
  12. {  
  13. Magic = 0;  
  14. NumberOfRvaAndSizes = 0;  
  15.  
  16. DataDirectory = new IMAGE_DATA_DIRECTORY[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];  
  17. }  
  18. }  

This seems to work, but if you use Marshal. SizeOf (typeof (IMAGE_OPTIONAL_HEADER), you will find that the length is basically different from the length defined in C/C ++. The problem still lies in the array in the structure. Although it seems that this array is defined in the structure, there is actually only one pointer to the IMAGE_DATA_DIRECTORY [] array type in this structure, the array content that should have been stored in the unknown DataDirectory is in the managed heap.
The problem is how to put the C # reference type array in a value type structure.

  1. Analysis of C # Insecure code
  2. Analysis on C # Calling ImageAnimator
  3. C # connect to Access and SQL Server databases
  4. C # fixed and active Variables
  5. Describes the value types in C #.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.