Solved

Finding the size of structures with array fields

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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