?
Solved

Finding the size of structures with array fields

Posted on 2006-10-20
3
Medium Priority
?
176 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 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