I work on a system that communicates with other systems via messages. Those messages are defined in a spec and every word must be exactly as defined. To accomplish this Ada allows me to define the fields of my record to specific words in memory. Will C++ allow me to accomplish this same task and if so could someone post an example
word = 4;
type Msg_Type1 is record
// ada allows me to rep out my record using the 'for use' clause
for Msg_Type1 use record
x at 0*word range 0..31;
y at 1*word range 0..15;
z at 1*word range 16..31;
Last edited by tsnofvdr; December 5th, 2012 at 01:13 PM.
To accomplish this Ada allows me to define the fields of my record to specific words in memory. Will C++ allow me to accomplish this same task and if so could someone post an example
I also don't know ADA, but what if this C++ example shows code that is hard-to-maintain or easily bug prone, so much so that no C++ programmer out in the field would recommend it? The point being that you should think on a higher level of abstraction. C++ isn't ADA, but the higher-level concept should be the same.
On a high-level, what exactly is being accomplished by taking this approach? Whatever that is, then you use C++ paradigms to accomplish the goal, where those paradigms may not look anything like ADA, may not even use structs, etc..
In general, attempting to translate language X to C++ almost verbatim usually doesn't work, is very cumbersome, or leads to serious runtime and maintenance issues (one such case is translating from Java to C++).
Last edited by Paul McKenzie; December 5th, 2012 at 01:51 PM.
if you need to have a system that will be portable (works for any c++ compiler), then don't use bitfields, instead do the actual maskign/shifting explicitely. How bits in a bitfield are assigned is up to the compiler and not all compilers work the same. Doing things yourself is a bit more work but it will ensure it works exactly how you need it to work.
You may also need to use structure packing. When creating a structure in C/C++, the compiler will insert padding bytes to make sure each field in a structure follows it's proper alignment, this may or may not match what ADA is doing.
for VS (and a few other compilers) this is arranged with the #pragma pack() directive. Other compilers solve this by other means.