-
December 5th, 2012, 01:36 PM
#1
Struct member manipulation
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
Code:
word = 4;
type Msg_Type1 is record
x: Some_Type;
y: Another_Type;
z: Another_Type;
end 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;
end record;
Last edited by tsnofvdr; December 5th, 2012 at 02:13 PM.
-
December 5th, 2012, 01:55 PM
#2
Re: Struct member manipulation
Sorry, I don't know Ada. You probably should explain just what you mean by "define how the compiler handles the fields of my record".
-
December 5th, 2012, 02:47 PM
#3
Re: Struct member manipulation
Originally Posted by tsnofvdr
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++).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 5th, 2012 at 02:51 PM.
-
December 6th, 2012, 02:09 AM
#4
Re: Struct member manipulation
You might want to look up 'bitfields':
http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx
http://en.wikipedia.org/wiki/Bit_field
But be aware that the implementation is compiler-dependent and as such not easily portable. Also, the memory layout differs on big-endian vs little-endian machines.
HTH
Richard
-
December 6th, 2012, 10:03 AM
#5
Re: Struct member manipulation
That's what I was looking for...Thanks for your help
-
December 7th, 2012, 05:09 AM
#6
Re: Struct member manipulation
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|