Solved

Finding the size of structures with array fields

Posted on 2006-10-20
3
171 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to Get total minutes between two hours 4 44
XML & .net 5 40
Allow space in this pattern 2 46
C# application error "Parameter Is Not Valid". 3 75
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now