473,545 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

determining the size of a structure

I have the following problem:

I'm developing a system where there are some processes that
communicate each other via message queues; the message one process can
send to another process is as follows:
*************** *************** ************
struct ST_MSG {
int iType;
char aData[MAX_DATA_PART_L ENGTH];
}
*************** *************** ************
the message is composed of two parts: iType tells about the kind of
message and the aData buffer contains the informative part.
depending on the value assumed by iType, the aData buffer has to be
cast explicitely to the proper structure.

the problem I have is that of determining the value of
MAX_DATA_PART_L ENGTH

I have tried using the #define macro of the preprocessor but, due to
the high number of structures, the compiler crashes.

I wouldn't like to use an off-line solution such as building a
separate project whose only job is that of calculating the
MAX_DATA_PART_L ENGTH number.

I would like to solve the problem during the compilation of my prject

do you have any idea?

Thank you

Luca
Jul 19 '05 #1
2 5411
"Luca" <lc***@libero.i t> wrote in message
news:a9******** *************** **@posting.goog le.com...
I have the following problem:

I'm developing a system where there are some processes that
communicate each other via message queues; the message one process can
send to another process is as follows:
*************** *************** ************
struct ST_MSG {
int iType;
char aData[MAX_DATA_PART_L ENGTH];
}
*************** *************** ************
the message is composed of two parts: iType tells about the kind of
message and the aData buffer contains the informative part.
depending on the value assumed by iType, the aData buffer has to be
cast explicitely to the proper structure.

the problem I have is that of determining the value of
MAX_DATA_PART_L ENGTH

I have tried using the #define macro of the preprocessor but, due to
the high number of structures, the compiler crashes.

I wouldn't like to use an off-line solution such as building a
separate project whose only job is that of calculating the
MAX_DATA_PART_L ENGTH number.

I would like to solve the problem during the compilation of my prject

do you have any idea?

Thank you

Luca


You mean MAX_DATA_PART_L ENGTH is not constant,
but must be calculated before a struct variable is created, right?
Then using a string can be a solution.

struct ST_MSG {
int iType;
string aData;
ST_MSG(int max) : iType(0), aData(string(ma x, 0)) { }
};

To create an object

int max = /* calculate max somehow */
ST_MSG s(max);

You can access each chars in aData as if it's an array, such as s.aData[10]
--
ES Kim
Jul 19 '05 #2
Luca wrote:
I have the following problem:

I'm developing a system where there are some processes that
communicate each other via message queues; the message one process can
send to another process is as follows:
*************** *************** ************
struct ST_MSG {
int iType;
char aData[MAX_DATA_PART_L ENGTH];
}
*************** *************** ************
the message is composed of two parts: iType tells about the kind of
message and the aData buffer contains the informative part.
depending on the value assumed by iType, the aData buffer has to be
cast explicitely to the proper structure.
I hope you don't actually intend to cast it. That is unlikely to result
in correct, portable, well-defined code.

the problem I have is that of determining the value of
MAX_DATA_PART_L ENGTH

I have tried using the #define macro of the preprocessor but, due to
the high number of structures, the compiler crashes.
Sounds like you need a better compiler. Anyway, you should use const
rather than #define for creating constants.

Your actual question doesn't make much sense. You can't create or use
instances of your structure without knowing the size. In the example
above, the size is MAX_DATA_PART_L ENGTH. That should be sufficient for
determining the size. You could also use sizeof(msg.aDat a) where msg is
an instance of ST_MSG.

I wouldn't like to use an off-line solution such as building a
separate project whose only job is that of calculating the
MAX_DATA_PART_L ENGTH number.

I would like to solve the problem during the compilation of my prject


Maybe I misunderstood the question at first. If you are asking what
value to give MAX_DATA_PART_L ENGTH, then I think it depends heavily on
what you are doing with it. If you have structs representing each of the
possible message types, you could create a union of all of them:

union SizeUnion
{
StringMessage s;
LongMessage l;
//...
};

Then the maximum size required to store any of those structures is
sizeof(SizeUnio n).

However, this is not necessarily a good solution. It relies on the
assumption that messages are no larger than the structures that are used
to represent them. This may be the case if you simply memcpy the data
from a struct into a char array and pass that along, but this is not a
very good solution to the problem - it is inflexible, confining,
non-portable (in the sense that structs may not be the same across
platforms or compilers), lends itself to errors (such as alignment
problems), difficult to extend, and sloppy.

The "correct" way to handle something like this is to define a protocol
for serializing your message types. That protocol would include details
about the sizes of the messages. This doesn't allow you to determine the
size at compile time, since the size is determined by the protocol. But
implemented correctly it is much easier to deal with, much more
flexible, and should give nice, clean results.

-Kevin

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
4909
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
2
2232
by: Phil Galey | last post by:
In VB.NET I find the IO object very handy in replacing most of the functionality of the FileSystemObject. One exception, however, is in determining the size of a file. How can you determine the size of a file in VB.NET without adding the FileSystemObject to your project?
12
7859
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
3
1581
by: Michael Geier | last post by:
Hello, suppose the following structure: __gc struct dfheader { int var1; int var2; unsigned var3; };
6
4993
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public class MyClass {
8
10148
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
19
2111
by: catmansa | last post by:
Is there anyway to determine the present pixel height & width size of a open browser window? :)
1
1493
by: FrankEBailey | last post by:
Hi, I have a limited number of ad slots on my website, let's say there are 5. I have lots of people wanting to advertise in those slots, let's say there are 100. They each join the queue for an ad slot on a first- come, first-served basis. I have been trying to find a way to determine the active date for a queued item. For the first five...
6
1291
by: Anthony Sox | last post by:
Hi all is there as way i can tell the amount of memory my object takes. for example if i have a collection of employee object say of 20, how much memory do they take e.g. 500kb. thank you.
0
7486
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7676
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7442
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7776
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3456
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
729
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.