Solved

Finding the size of structures with array fields

Posted on 2006-10-20
3
175 Views
Last Modified: 2010-04-16
I'm creating what will be a large, multidimensional array of structures, so I want the elements to be as stream-lined as possible. The problem is that the structure contains an array, wich cannot be given a set size. Will the following work to 'cap' the array field and use as little memory as possible?

struct example
   {
       int field1;
       bool[] field2;
   }

public example[,,] array = new example[30, 30, 30];

void Initialize array();
   {
       for(a=0; a<30; a++) {
             for(b=0; b<30, b++) {
                    for(c=0; c<30; c++) {
                           array[a,b,c].field2 = new bool[7];
                    {
              {
        {
   {
0
Comment
Question by:Fozzz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 17779012
This code is correct. To save memory, you can use BitArray class. However, using bit fields reduces performance, and you need to estimate possible results: replace bool array with BitField only if memory is really serious issue.
0
 

Author Comment

by:Fozzz
ID: 17781351
Thank you. would it be asking too much to ask if you know of a 'sizeof' equivalent that will work with structure types and not just primitive value types?...
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17782836
There is Marshal.SizeOf function. But if structure contains reference type (like array), it gives only 4 bytes in sizeof - size of 32-bits pointer.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question