Solved

Finding the size of structures with array fields

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now