Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9208149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:33:18+00:00 2026-06-18T00:33:18+00:00

I have the following C structure struct MyStruct { char chArray[96]; __int64 offset; unsigned

  • 0

I have the following C structure

struct MyStruct {
    char chArray[96];
    __int64 offset;
    unsigned count;
}

I now have a bunch of files created in C with thousands of those structures. I need to read them using C# and speed is an issue.

I have done the following in C#

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Size = 108)]
public struct PreIndexStruct {
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 96)]
    public string Key;
    public long Offset;
    public int Count;
}

And then I read the data from the file using

using (BinaryReader br = new BinaryReader(
       new FileStream(pathToFile, FileMode.Open, FileAccess.Read, 
                      FileShare.Read, bufferSize))) 
{
    long length = br.BaseStream.Length;
    long position = 0;

    byte[] buff = new byte[structSize];
    GCHandle buffHandle = GCHandle.Alloc(buff, GCHandleType.Pinned);
    while (position < length) {
        br.Read(buff, 0, structSize);
        PreIndexStruct pis = (PreIndexStruct)Marshal.PtrToStructure(
            buffHandle.AddrOfPinnedObject(), typeof(PreIndexStruct));
        structures.Add(pis);

        position += structSize;
    }
    buffHandle.Free();
}

This works perfectly and I can retrieve the data just fine from the files.

I’ve read that I can speedup things if instead of using GCHandle.Alloc/Marshal.PtrToStructure I use C++/CLI or C# unsafe code. I found some examples but they only refer to structures without fixed sized arrays.

My question is, for my particular case, is there a faster way of doing things with C++/CLI or C# unsafe code?

EDIT

Additional performance info (I’ve used ANTS Performance Profiler 7.4):

66% of my CPU time is used by calls to Marshal.PtrToStructure.

Regarding I/O, only 6 out of 105ms are used to read from the file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:33:18+00:00Added an answer on June 18, 2026 at 12:33 am

    In this case, you don’t explicitly need to use P/Invoke since you don’t have to pass the struct back and forth between managed and native code. So you could do this instead. It would avoid this useless GC handle allocation, and allocate only what’s needed.

    public struct PreIndexStruct {
        public string Key;
        public long Offset;
        public int Count;
    }
    
    while (...) {
        ...
        PreIndexStruct pis = new PreIndexStruct();
        pis.Key = Encoding.Default.GetString(reader.ReadBytes(96));
        pis.Offset = reader.ReadInt64();
        pis.Count = reader.ReadInt32();
        structures.Add(pis);
    }
    

    I’m not sure you can be much faster than this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created following structure 'data' in C typedef struct data { double *dattr;
I have the following array structure (linked list): struct str_pair { char ip [50]
I have following structure typedef struct List_Node { struct File_Descriptor *data; char *key; struct
I have the following structure: struct hashItem { char userid[8]; char name[30]; struct hashItem
Suppose I have the following structure: typedef struct { unsigned field1 :1; unsigned field2
Say I have the following C structure definition: struct stringStructure { char *stringVariable; };
I have the following structure typedef struct DeviceInfo { char[30] name; char[30] serial Number;
I have the following data structure: struct file{ char name[MAX_FILE_NAME]; char data[BLOCK_SIZE - MAX_FILE_NAME]
I have defined the following structure below. typedef union { struct { unsigned command:15;
I have the following structure and function that adds things to the structure: struct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.