473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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_LENGTH];
}
******************************************
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_LENGTH

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_LENGTH 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 5399
"Luca" <lc***@libero.it> wrote in message
news:a9*************************@posting.google.co m...
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_LENGTH];
}
******************************************
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_LENGTH

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_LENGTH 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_LENGTH 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(max, 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_LENGTH];
}
******************************************
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_LENGTH

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_LENGTH. That should be sufficient for
determining the size. You could also use sizeof(msg.aData) 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_LENGTH 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_LENGTH, 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(SizeUnion).

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
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
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...
12
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
3
by: Michael Geier | last post by:
Hello, suppose the following structure: __gc struct dfheader { int var1; int var2; unsigned var3; };
6
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...
8
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". */...
19
by: catmansa | last post by:
Is there anyway to determine the present pixel height & width size of a open browser window? :)
1
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...
6
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.